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.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
Alice and Bob are playing a game where they take turns removing stones from a pile, with Alice going first.
The player who cannot make a move loses the game.
Given a positive integer n, return true if Alice wins the game and false otherwise.
Example 1:
Input: n = 12
Output: true
Explanation:
Example 2:
Input: n = 1
Output: false
Explanation:
Constraints:
1 <= n <= 50Problem summary: Alice and Bob are playing a game where they take turns removing stones from a pile, with Alice going first. Alice starts by removing exactly 10 stones on her first turn. For each subsequent turn, each player removes exactly 1 fewer stone than the previous opponent. The player who cannot make a move loses the game. Given a positive integer n, return true if Alice wins the game and false otherwise.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
12
1
stone-game-iv)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3360: Stone Removal Game
class Solution {
public boolean canAliceWin(int n) {
int x = 10, k = 0;
while (n >= x) {
n -= x;
--x;
++k;
}
return k % 2 == 1;
}
}
// Accepted solution for LeetCode #3360: Stone Removal Game
func canAliceWin(n int) bool {
x, k := 10, 0
for n >= x {
n -= x
x--
k++
}
return k%2 == 1
}
# Accepted solution for LeetCode #3360: Stone Removal Game
class Solution:
def canAliceWin(self, n: int) -> bool:
x, k = 10, 0
while n >= x:
n -= x
x -= 1
k += 1
return k % 2 == 1
// Accepted solution for LeetCode #3360: Stone Removal Game
// Rust example auto-generated from java 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 (java):
// // Accepted solution for LeetCode #3360: Stone Removal Game
// class Solution {
// public boolean canAliceWin(int n) {
// int x = 10, k = 0;
// while (n >= x) {
// n -= x;
// --x;
// ++k;
// }
// return k % 2 == 1;
// }
// }
// Accepted solution for LeetCode #3360: Stone Removal Game
function canAliceWin(n: number): boolean {
let [x, k] = [10, 0];
while (n >= x) {
n -= x;
--x;
++k;
}
return k % 2 === 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.