Ctrl K

Last One After the Sweeps

Medium
The numbers 1 to n are written in increasing order. They are swept in passes until only one number is left. - The first pass goes left to right: it crosses out the first number and then every other number after it (the third, the fifth, and so on) to the end of the row. - The second pass goes right to left: it crosses out the rightmost number and then every other number while moving left. - After that the directions keep alternating. Return the number that remains at the end.
Example 1
n=10 → 8

[1..10] → sweeping left to right removes 1, 3, 5, 7, 9: [2, 4, 6, 8, 10] → sweeping right to left removes 10, 6, 2: [4, 8] → sweeping left to right removes 4: [8].

Example 2
n=6 → 4

[1..6] → [2, 4, 6] → sweeping right to left removes 6 and 2: [4].

Example 3
n=1 → 1
Constraints
  • 1 ≤ n ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsAdding Digit Lists MediumFast Exponentiation MediumEvery Bracketing Result MediumDuel for the End Cards MediumDoubling Rows of Bits Medium
loading Python…
Input
n=10
Expected
8

Run checks the first 3 tests in your browser. Submit checks all 80 tests on the server, hidden ones included.