Leetcode 390. Elimination Game
You are given an integer n. An elimination game is played with this sequence: start with the list [1, 2, 3, …, n]. At every step, eliminate every second number until only one number remains. You must find this last remaining number.
Here’s how the game is played:
Example:
Input: n = 9
Output: 6
Explanation:
Start with [1, 2, 3, 4, 5, 6, 7, 8, 9]
After removing every second element (left-to-right): [2, 4, 6, 8]
After removing every second element (right-to-left): [2, 6]
After removing every second element (left-to-right): [6]
n
is always a positive integer.n
?
n
can be any positive integer up to (10^9).To solve this problem efficiently, a direct simulation approach where we generate and eliminate elements is impractical for large values of n due to time and space constraints. Instead, we can use a mathematical approach:
Tail Recursive Approach:
The time complexity is O(log n) due to iterative halving of the sequence length with each pass.
Here’s the Java code for this approach:
public class Solution {
public int lastRemaining(int n) {
boolean left = true; // Starting direction
int head = 1; // The head of the sequence
int step = 1; // The initial step size
int remaining = n; // The remaining count of numbers
while (remaining > 1) {
// If we eliminate from the left, or we're at an odd count, the head moves
if (left || remaining % 2 == 1) {
head += step;
}
// Halve the number of remaining elements
remaining /= 2;
// Double the step size (as we shrink the sequence)
step *= 2;
// Alternate the direction
left = !left;
}
return head;
}
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println(sol.lastRemaining(9)); // Output should be 6
}
}
head
starts at 1, representing the first element of the sequence.head
depending on the direction and whether the count of remaining elements is odd.remaining
number of elements.step
to reflect the distance between the first element of each subsequent sequence.left
variable to alternate the elimination direction.By following this optimized approach, the solution efficiently determines the last remaining number even for very large values of n
.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?