We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Strange Counter
  5. Discussions

Strange Counter

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 810 Discussions, By:

votes

Please Login in order to post a comment

  • sreejith_menon
    6 years ago+ 71 comments

    My simple solution of the order log n, it passed all test cases. My logic was that there was a pattern between the actual time and the time displayed on the stop watch. Am I missing something here? Many of the solutions here seem to take complex approaches.

    rem = 3
    while t > rem:
        t = t-rem
        rem *= 2
    
    print(rem-t+1)
    
    330|
    Permalink
    View more Comments..
  • wooddoo
    6 years ago+ 3 comments

    This is actually a math problem. Assume t is in the cycle with size 3*2^k, then k = floor(log2(t/3+1)). So, the problem is O(1).

    39|
    Permalink
  • tao_zhang
    6 years ago+ 7 comments

    A Java solution:

    import java.util.Scanner;
    
    public class StrangeCounter {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            long t = in.nextLong(), n = 2;
            while (3 * (n - 1) < t) n = 2 * n;
            System.out.println((3 * (n - 1) - t + 1));
        }
    }
    
    22|
    Permalink
    View more Comments..
  • nikhilkuria
    6 years ago+ 3 comments

    Really implore fellow coders not to paste readymade solution in the board.

    This is a real nice problem. If you can understand how the value is calculated and can find a pattern in there, you are already there.

    20|
    Permalink
  • M180258CA_SID
    3 years ago+ 5 comments

    here is the trick

    long strangeCounter(long t) {
        long v=4;
    
        while(v<=t)
        {
            v=v*2+2;
        }
        return v-t;
    }
    

    just it :)

    18|
    Permalink
    View more Comments..
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature