Search⌘ K
AI Features

Solution: Nim Game

Understand the mathematical pattern behind the Nim Game to identify winning moves quickly. Learn to use modulo 4 to decide if you can win against an optimal opponent. This lesson teaches a clear and efficient approach with constant time and space complexity, helping you solve similar game theory problems confidently.

Statement

You and a friend are playing a game called the Nim Game with a heap of stones. The rules are as follows:

  • There is a single heap containing n stones.

  • You and your friend take alternating turns, with you going first.

  • On each turn, the current player must remove between ...