Nim — Who Wins?
Difficulty: Easy · Topic: Game theory
In Nim, players alternate removing any positive number of stones from a single pile; the player who takes the last stone wins. Given the pile sizes, return True if the first player wins with perfect play. The theorem: the first player wins iff the XOR of all pile sizes is non-zero.
Examples
nim_winner([1,2,3])
→ false1^2^3 = 0 → first player loses.
nim_winner([1,2,4])
→ true1^2^4 = 7 ≠ 0 → first player wins.
Target complexity: time O(n), space O(1)
Write your solution in Python and run it against hidden tests right on this page. Progressive hints and Python + C++ reference solutions are included.
Learn the idea first: the lesson for this problem.