The Classic Game of Nim

Alice and Bob are playing the ancient mathematical game of Nim. The game starts with n piles of stones, where each pile contains a certain number of stones. Players take turns removing stones from the piles, with Alice going first.

On each turn, a player must:
โ€ข Choose any non-empty pile
โ€ข Remove any positive number of stones from that pile (from 1 stone up to all remaining stones)

The player who cannot make a move (because all piles are empty) loses the game. Both players play optimally, meaning they always make the best possible move.

Given an integer array piles where piles[i] represents the number of stones in the i-th pile, determine if Alice can guarantee a win. Return true if Alice has a winning strategy, false if Bob will win with optimal play.

Input & Output

example_1.py โ€” Basic winning position
$ Input: piles = [1, 3, 5, 4]
โ€บ Output: true
๐Ÿ’ก Note: Alice can win. The nim-sum is 1โŠ•3โŠ•5โŠ•4 = 7, which is non-zero. Alice can make a move to force Bob into a losing position (nim-sum = 0).
example_2.py โ€” Balanced losing position
$ Input: piles = [2, 7, 5]
โ€บ Output: true
๐Ÿ’ก Note: Alice wins because nim-sum = 2โŠ•7โŠ•5 = 0โŠ•5 = 5 โ‰  0. Any non-zero nim-sum means the current player can force a win.
example_3.py โ€” Perfect losing position
$ Input: piles = [3, 4, 7]
โ€บ Output: false
๐Ÿ’ก Note: Alice loses because nim-sum = 3โŠ•4โŠ•7 = 7โŠ•7 = 0. When nim-sum is 0, the current player is in a losing position with optimal play.

Visualization

Tap to expand
Why XOR Works in Nim GameExample: [3, 5, 6]3 = 0115 = 1016 = 110โŠ• = 000Nim-sum = 0 โ†’ Alice LOSESWhy This Works:โ€ข XOR has special symmetry propertiesโ€ข From nim-sum โ‰  0: can always reach 0โ€ข From nim-sum = 0: any move makes โ‰  0โ€ข Game always terminates at nim-sum = 0โ€ข Player facing 0 always loses!The Winning Strategy1. Calculate nim-sum: XOR all pile sizes2. If nim-sum = 0: You're in a losing position (sorry!)3. If nim-sum โ‰  0: You can guarantee a win!4. How to win: Find pile wherepile_size โŠ• nim_sum < pile_size5. Remove stones to make that pileequal to pile_size โŠ• nim_sumThis forces your opponent into nim-sum = 0 (losing position)
Understanding the Visualization
1
Binary Representation
Convert each pile size to binary and align them in columns
2
Column-wise XOR
XOR each binary column independently (even 1s become 0, odd 1s become 1)
3
Interpret Result
If result is all zeros, current player loses; otherwise they can win
4
Winning Strategy
From non-zero nim-sum, always exists a move to make nim-sum zero for opponent
Key Takeaway
๐ŸŽฏ Key Insight: XOR captures the 'balance' of the game - when balanced (XOR = 0), you lose; when unbalanced, you can always rebalance it in your favor!

Time & Space Complexity

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

Single pass through all piles to compute XOR

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

Only using a single variable to store the nim-sum

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค piles.length โ‰ค 7
  • 1 โ‰ค piles[i] โ‰ค 7
  • Both players play optimally
  • Alice always goes first
Asked in
Google 25 Microsoft 18 Amazon 15 Meta 12
28.5K Views
Medium Frequency
~15 min Avg. Time
892 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