LeetCode Problem Workspace

Elimination Game

Elimination Game uses a systematic removal of numbers with alternating left-right passes, solvable with math and recursion.

category

2

Topics

code_blocks

4

Code langs

hub

3

Related

Practice Focus

Medium · Math plus Recursion

bolt

Answer-first summary

Elimination Game uses a systematic removal of numbers with alternating left-right passes, solvable with math and recursion.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Math plus Recursion

Try AiBox Copilotarrow_forward

The optimal solution for Elimination Game leverages recursion combined with mathematical observation about number elimination patterns. Instead of simulating each step, we track the first element, step size, and direction. This approach reduces both time and space usage while directly computing the last remaining number.

Problem Statement

You are given an integer n representing a list of numbers from 1 to n arranged in increasing order. Repeatedly eliminate numbers in rounds: remove the first number and every other number from left to right, then reverse the direction and repeat the process until only one number remains.

Return the final number that remains after all elimination rounds. For example, given n = 9, the sequence progresses as [1,2,3,4,5,6,7,8,9] → [2,4,6,8] → [2,6] → [6], so the result is 6.

Examples

Example 1

Input: n = 9

Output: 6

arr = [1, 2, 3, 4, 5, 6, 7, 8, 9] arr = [2, 4, 6, 8] arr = [2, 6] arr = [6]

Example 2

Input: n = 1

Output: 1

Example details omitted.

Constraints

  • 1 <= n <= 109

Solution Approach

Recursive Tracking of First Element

Maintain the first number, step size, and direction of elimination. Update the first element each round based on whether elimination occurs from left or right, and halve the effective length until only one number remains.

Mathematical Pattern Recognition

Observe that the first element always changes in a predictable pattern: it increases by step size when eliminated from left, and from right if the remaining count is odd. Exploit this to skip explicit array manipulation.

Combine Recursion and Step Calculation

Use a recursive function to handle rounds by adjusting the first number and step size. Each recursive call represents a new elimination round, reducing problem size logarithmically until the base case of one element is reached.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(log n) because each round halves the number of elements. Space complexity is O(log n) due to recursion stack, with no full array simulation needed.

What Interviewers Usually Probe

  • Candidate tries simulating the array fully instead of recognizing elimination patterns.
  • Candidate identifies the left-right elimination pattern and updates first element systematically.
  • Candidate applies recursion with step tracking to reach the final number efficiently.

Common Pitfalls or Variants

Common pitfalls

  • Simulating the full array leads to TLE for large n.
  • Forgetting that the first element moves differently when eliminating from right with odd count.
  • Confusing step updates between rounds and miscalculating the first remaining number.

Follow-up variants

  • Modify elimination to remove every k-th element instead of every other, requiring adjustment to step computation.
  • Start elimination from right first, which changes the first element update logic slightly.
  • Return the sequence of remaining numbers after each round instead of just the last number.

FAQ

What is the optimal approach for Elimination Game?

The optimal approach combines recursion with tracking the first element and step size, avoiding full array simulation.

Why can't I just simulate the array to solve this problem?

Full simulation is inefficient and can exceed time limits for large n; mathematical pattern recognition is required.

How does direction of elimination affect the first element?

From left, the first element always moves by step size. From right, it moves only if remaining count is odd.

Can this method handle very large n, like 10^9?

Yes, recursive step tracking avoids large array creation, handling n up to 10^9 efficiently.

Which patterns does the Elimination Game solution rely on?

It relies on alternating left-right elimination and predictable updates of the first element each round.

terminal

Solution

Solution 1

#### Python3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution:
    def lastRemaining(self, n: int) -> int:
        a1, an = 1, n
        i, step, cnt = 0, 1, n
        while cnt > 1:
            if i % 2:
                an -= step
                if cnt % 2:
                    a1 += step
            else:
                a1 += step
                if cnt % 2:
                    an -= step
            cnt >>= 1
            step <<= 1
            i += 1
        return a1
Elimination Game Solution: Math plus Recursion | LeetCode #390 Medium