Nim Game - Problem
The Ultimate Nim Game Challenge!

Imagine you're sitting across from your friend with a pile of stones between you, about to engage in one of the oldest strategic games known to humanity - Nim! ๐Ÿชจ

Here's how it works: You have a heap containing n stones on the table. You and your friend take turns removing stones, and you go first. On each turn, a player must remove between 1 to 3 stones from the heap. The player who takes the last stone wins the game! ๐Ÿ†

Your Mission: Given the number of stones n, determine if you can guarantee a win assuming both players play optimally (meaning everyone makes the best possible move at each turn).

Input: An integer n representing the initial number of stones
Output: true if you can guarantee a win, false otherwise

Example: If there are 4 stones, no matter what you do (take 1, 2, or 3), your opponent can always take the remaining stones and win!

Input & Output

example_1.py โ€” Small winning position
$ Input: n = 1
โ€บ Output: true
๐Ÿ’ก Note: You can take 1 stone and win immediately since you took the last stone.
example_2.py โ€” Classic losing position
$ Input: n = 4
โ€บ Output: false
๐Ÿ’ก Note: No matter how many stones you take (1, 2, or 3), your opponent can take all remaining stones in their turn. Take 1โ†’3 left (opponent takes all), Take 2โ†’2 left (opponent takes all), Take 3โ†’1 left (opponent takes it).
example_3.py โ€” Force opponent into losing position
$ Input: n = 5
โ€บ Output: true
๐Ÿ’ก Note: You can take 1 stone, leaving 4 stones. Now your opponent is in a losing position (multiple of 4), and no matter what they do, you can win.

Visualization

Tap to expand
Nim Game Strategic PositionsWINn % 4 โ‰  0LOSEn % 4 = 0Force opponent into multiple of 4Winning Strategy1. If n % 4 = 0: You're in a losing position2. If n % 4 = 1: Take 1 stone (leave multiple of 4)3. If n % 4 = 2: Take 2 stones (leave multiple of 4)4. If n % 4 = 3: Take 3 stones (leave multiple of 4)Example: n=13 โ†’ 13%4=1 โ†’ Take 1 โ†’ Leave 12 (opponent loses!)The opponent will be stuck: no matter what they take (1,2,3),you can always respond to put them back in a multiple-of-4 position!
Understanding the Visualization
1
Pattern Discovery
Test small values to discover the losing pattern
2
Strategic Analysis
Understand why multiples of 4 are trapped positions
3
Optimal Strategy
Always leave your opponent with a multiple of 4 stones
Key Takeaway
๐ŸŽฏ Key Insight: Positions where n % 4 = 0 are losing positions because any move you make can be countered to maintain the opponent's advantage. The game becomes a race to force your opponent into these 'trap' positions!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Single modulo operation

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only uses a constant amount of extra space

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค n โ‰ค 231 - 1
  • Both players play optimally
  • You always go first
Asked in
Adobe 25 Amazon 15 Google 12 Microsoft 8
38.3K Views
Medium Frequency
~5 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen