Elimination Game
Medium Recursion
The whole numbers from 1 to n stand in a row, smallest first.
Rounds of crossing out follow, until only one number is left. The first round goes from left to right: it crosses out the first number still standing, skips the next, crosses out the one after, and so on to the end of the row. The second round does the same from right to left, starting with the last number still standing. The rounds go on like that, turning round each time.
Return the number that is left at the end.
Example 1
From the left, 1, 3, 5, 7 and 9 go, leaving 2, 4, 6, 8 and 10. From the right, 10, 6 and 2 go, leaving 4 and 8. From the left again, 4 goes, and 8 is left.
Example 2
Example 3
Constraints
1 ≤ n ≤ 10⁹
Solve it here
Write it in Python, Java or C++, run it on your own cases, and submit against hidden tests.
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
See the AI roundMore Recursion
- Permutation SequenceAI round · Hard
- Count Good NumbersAI round · Medium
- Find Kth Bit in Nth Binary StringAI round · Medium
- Pow(x, n)Medium
- Power of ThreeEasy