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.
Strange Counter
Strange Counter
+ 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)
+ 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).
+ 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)); } }
+ 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.
+ 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 :)
Load more conversations
Sort 810 Discussions, By:
Please Login in order to post a comment