> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Elimination Game Python Solution with Tests

> Tested Python solution for LeetCode 390 with 34 pytest cases. Generate a practice environment with lcpy.

LeetCode 390, [Medium](/catalog/medium). Topics: [Math](/catalog/topics/math), [Recursion](/catalog/topics/recursion). [View on LeetCode](https://leetcode.com/problems/elimination-game/description/).

Generate this problem as a practice environment: tested reference solution, 34 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 390   # by problem number
lcpy gen -s elimination_game   # by problem name
```

## Problem

You have a list \<code>arr\</code> of all integers in the range \<code>\[1, n]\</code> sorted in a strictly increasing order. Apply the following algorithm on \<code>arr\</code>:

\<ul>
\<li>Starting from left to right, remove the first number and every other number afterward until you reach the end of the list.\</li>
\<li>Repeat the previous step again, but this time from right to left, remove the rightmost number and every other number from the remaining numbers.\</li>
\<li>Keep repeating the steps again, alternating left to right and right to left, until a single number remains.\</li>
\</ul>

\<p>Given the integer \<code>n\</code>, return \<em>the last number that remains in\</em> \<code>arr\</code>.\</p>

### Examples

```
Input: n = 9
Output: 6
Explanation:
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
arr = [2, 4, 6, 8]
arr = [2, 6]
arr = [6]
```

```
Input: n = 1
Output: 1
```

### Constraints

* 1 \<= n \<= 10\<sup>9\</sup>

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/elimination_game/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/elimination_game/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(log n)
    # Space: O(1)
    def last_remaining(self, n: int) -> int:
        head, step, left = 1, 1, True
        while n > 1:
            if left or n % 2 == 1:
                head += step
            step *= 2
            n //= 2
            left = not left
        return head
```

## Complexity

| Time | Space |
| - | - |
| O(log n) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.