Kitchen Timer
InterviewTime limit2sMemory limit1024 MB
Split x into the fewest terms of the form 2^k - 1 (k at least 1) and output the number of terms minus one.
- Level
Medium5 of 10
- Topics
- Greedy, Math, Bit manipulation
- Solved
- No attempts yet
Problem
Kenny has a microwave in his kitchen. Its timer uses a single button with an unusual interface. You press the button one or more times to start heating. The first press sets the timer to minute. If you press again immediately, minutes are added, for a total of minutes. Another immediate press adds more minutes, and so on. If you press the button for the -th time without a pause, it adds minutes to the timer.
It seems impossible to set some amounts of time with the button. For example, how do you set the timer for minutes? You can reset the press counter by pausing for one second. If you press, pause for one second, and then press again, the timer is set to minutes. Another example: if you press, press, pause, press, press, press, the total time is minutes.
Kenny needs to heat his food for exactly minutes. Find the minimum number of one-second pauses he needs to set the timer. Only pauses take time. The time spent pressing the button is ignored.
Input
The first line contains the number of test cases (). Each of the next lines contains a single integer (), the number of minutes Kenny needs to heat his food.
Output
For each test case, print the minimum number of one-second pauses needed to set the timer to minutes, one answer per line.
Hint
In the first test case, no pauses are needed. Kenny can press the button once.
In the second test case, Kenny can press, pause, press to set the timer to minutes.
In the third test case, Kenny can press the button twice to set the timer to minutes.
In the fourth test case, Kenny can press, press, pause, press to set the timer to minutes.