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.
You can just calculate the answer without the while loop:
val cycleLength = (Math.pow(2, log((t.toLong + 2) / 3, 2)) * 3).toLong;
val answer = cycleLength + cycleLength - t - 2;
Note that the log function, here, truncates to only the integer part (i.e., returns the floor of the log base 2).
At the start of each cycle, the value is 3 times some power of 2, and that value is t plus 2.
Then each successive value decrements the starting value by one, until it reaches one, then a new cycle starts.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Strange Counter
You are viewing a single comment's thread. Return to all comments →
You can just calculate the answer without the while loop:
Note that the log function, here, truncates to only the integer part (i.e., returns the floor of the log base 2).
At the start of each cycle, the value is 3 times some power of 2, and that value is t plus 2. Then each successive value decrements the starting value by one, until it reaches one, then a new cycle starts.