Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
You have a list arr of all integers in the range [1, n] sorted in a strictly increasing order. Apply the following algorithm on arr:
Given the integer n, return the last number that remains in arr.
Example 1:
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]
Example 2:
Input: n = 1 Output: 1
Constraints:
1 <= n <= 109Problem summary: You have a list arr of all integers in the range [1, n] sorted in a strictly increasing order. Apply the following algorithm on arr: Starting from left to right, remove the first number and every other number afterward until you reach the end of the list. Repeat the previous step again, but this time from right to left, remove the rightmost number and every other number from the remaining numbers. Keep repeating the steps again, alternating left to right and right to left, until a single number remains. Given the integer n, return the last number that remains in arr.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
9
1
min-max-game)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #390: Elimination Game
class Solution {
public int lastRemaining(int n) {
int a1 = 1, an = n, step = 1;
for (int i = 0, cnt = n; cnt > 1; cnt >>= 1, step <<= 1, ++i) {
if (i % 2 == 1) {
an -= step;
if (cnt % 2 == 1) {
a1 += step;
}
} else {
a1 += step;
if (cnt % 2 == 1) {
an -= step;
}
}
}
return a1;
}
}
// Accepted solution for LeetCode #390: Elimination Game
func lastRemaining(n int) int {
a1, an, step := 1, n, 1
for i, cnt := 0, n; cnt > 1; cnt, step, i = cnt>>1, step<<1, i+1 {
if i%2 == 1 {
an -= step
if cnt%2 == 1 {
a1 += step
}
} else {
a1 += step
if cnt%2 == 1 {
an -= step
}
}
}
return a1
}
# Accepted solution for LeetCode #390: Elimination Game
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
// Accepted solution for LeetCode #390: Elimination Game
struct Solution;
impl Solution {
fn last_remaining(n: i32) -> i32 {
if n == 1 {
1
} else {
2 * (1 + n / 2 - Self::last_remaining(n / 2))
}
}
}
#[test]
fn test() {
let n = 9;
let res = 6;
assert_eq!(Solution::last_remaining(n), res);
}
// Accepted solution for LeetCode #390: Elimination Game
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #390: Elimination Game
// class Solution {
// public int lastRemaining(int n) {
// int a1 = 1, an = n, step = 1;
// for (int i = 0, cnt = n; cnt > 1; cnt >>= 1, step <<= 1, ++i) {
// if (i % 2 == 1) {
// an -= step;
// if (cnt % 2 == 1) {
// a1 += step;
// }
// } else {
// a1 += step;
// if (cnt % 2 == 1) {
// an -= step;
// }
// }
// }
// return a1;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.