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.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given an integer n.
We write the integers from 1 to n in a sequence from left to right. Then, alternately apply the following two operations until only one integer remains, starting with operation 1:
Return the last remaining integer.
Example 1:
Input: n = 8
Output: 3
Explanation:
[1, 2, 3, 4, 5, 6, 7, 8] in a sequence.[1, 2, 3, 4, 5, 6, 7, 8]. The remaining integers are [1, 3, 5, 7].[1, 3, 5, 7]. The remaining integers are [3, 7].[3, 7]. The remaining integer is [3].Example 2:
Input: n = 5
Output: 1
Explanation:
[1, 2, 3, 4, 5] in a sequence.[1, 2, 3, 4, 5]. The remaining integers are [1, 3, 5].[1, 3, 5]. The remaining integers are [1, 5].[1, 5]. The remaining integer is [1].Example 3:
Input: n = 1
Output: 1
Explanation:
[1] in a sequence.Constraints:
1 <= n <= 1015Problem summary: You are given an integer n. We write the integers from 1 to n in a sequence from left to right. Then, alternately apply the following two operations until only one integer remains, starting with operation 1: Operation 1: Starting from the left, delete every second number. Operation 2: Starting from the right, delete every second number. Return the last remaining integer.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
8
5
1
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3782: Last Remaining Integer After Alternating Deletion Operations
// Auto-generated Java example from go.
class Solution {
public void exampleSolution() {
}
}
// Reference (go):
// // Accepted solution for LeetCode #3782: Last Remaining Integer After Alternating Deletion Operations
// package main
//
// // https://space.bilibili.com/206214
// func lastInteger1(n int64) int64 {
// start, d := int64(1), int64(1) // 等差数列首项,公差
// for ; n > 1; n = (n + 1) / 2 {
// start += (n - 2 + n%2) * d
// d *= -2
// }
// return start
// }
//
// // https://oeis.org/A090569
// func lastInteger(n int64) int64 {
// const mask = 0xAAAAAAAAAAAAAAA // ...1010
// return (n-1)&mask + 1 // 取出 n-1 的从低到高第 2,4,6,... 位,最后再加一(从 1 开始)
// }
// Accepted solution for LeetCode #3782: Last Remaining Integer After Alternating Deletion Operations
package main
// https://space.bilibili.com/206214
func lastInteger1(n int64) int64 {
start, d := int64(1), int64(1) // 等差数列首项,公差
for ; n > 1; n = (n + 1) / 2 {
start += (n - 2 + n%2) * d
d *= -2
}
return start
}
// https://oeis.org/A090569
func lastInteger(n int64) int64 {
const mask = 0xAAAAAAAAAAAAAAA // ...1010
return (n-1)&mask + 1 // 取出 n-1 的从低到高第 2,4,6,... 位,最后再加一(从 1 开始)
}
# Accepted solution for LeetCode #3782: Last Remaining Integer After Alternating Deletion Operations
#
# @lc app=leetcode id=3782 lang=python3
#
# [3782] Last Remaining Integer After Alternating Deletion Operations
#
# @lc code=start
class Solution:
def lastInteger(self, n: int) -> int:
head = 1
step = 1
remaining = n
is_left = True
while remaining > 1:
if is_left:
# Starting from left, we always keep the first element (head)
# deleting every second number means we keep indices 0, 2, 4...
# So head remains unchanged.
pass
else:
# Starting from right
# If remaining count is odd, the first element survives
# (e.g., [1, 2, 3] -> keep 3, del 2, keep 1)
# If remaining count is even, the first element is deleted
# (e.g., [1, 2, 3, 4] -> keep 4, del 3, keep 2, del 1)
if remaining % 2 == 0:
head += step
step *= 2
remaining = (remaining + 1) // 2
is_left = not is_left
return head
# @lc code=end
// Accepted solution for LeetCode #3782: Last Remaining Integer After Alternating Deletion Operations
// Rust example auto-generated from go reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (go):
// // Accepted solution for LeetCode #3782: Last Remaining Integer After Alternating Deletion Operations
// package main
//
// // https://space.bilibili.com/206214
// func lastInteger1(n int64) int64 {
// start, d := int64(1), int64(1) // 等差数列首项,公差
// for ; n > 1; n = (n + 1) / 2 {
// start += (n - 2 + n%2) * d
// d *= -2
// }
// return start
// }
//
// // https://oeis.org/A090569
// func lastInteger(n int64) int64 {
// const mask = 0xAAAAAAAAAAAAAAA // ...1010
// return (n-1)&mask + 1 // 取出 n-1 的从低到高第 2,4,6,... 位,最后再加一(从 1 开始)
// }
// Accepted solution for LeetCode #3782: Last Remaining Integer After Alternating Deletion Operations
// Auto-generated TypeScript example from go.
function exampleSolution(): void {
}
// Reference (go):
// // Accepted solution for LeetCode #3782: Last Remaining Integer After Alternating Deletion Operations
// package main
//
// // https://space.bilibili.com/206214
// func lastInteger1(n int64) int64 {
// start, d := int64(1), int64(1) // 等差数列首项,公差
// for ; n > 1; n = (n + 1) / 2 {
// start += (n - 2 + n%2) * d
// d *= -2
// }
// return start
// }
//
// // https://oeis.org/A090569
// func lastInteger(n int64) int64 {
// const mask = 0xAAAAAAAAAAAAAAA // ...1010
// return (n-1)&mask + 1 // 取出 n-1 的从低到高第 2,4,6,... 位,最后再加一(从 1 开始)
// }
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.