Short Problem Definition:
Bob has a strange counter. At the first second, it displays the number 3. Each second, the number displayed by the counter decrements by 1 until it reaches 1.
The counter counts down in cycles. In next second, the timer resets to 2x the initial number for the prior cycle and continues counting down.
time complexity is O(log(N))
space complexity is O(1)
This is a ‘simple’ mathematical problem that requires no while loops. First, find out what cycle the value T belongs to. Secondly, determine the value at the end of the cycle. Go back T steps to figure out the exact value at T.
Be careful, this can easily int overflow in C++, if the input is large enough.