Search⌘ K
AI Features

Solution: Nim Game

Understand how to apply mathematical patterns and game theory to the Nim Game. Learn to identify winning positions by using modulo calculations, enabling you to determine if the first player can win when both players play optimally. This lesson explains the strategy behind the solution and highlights its constant time and space complexity.

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 ...