Search⌘ K
AI Features

Solution: Nim Game

Understand the mathematical pattern in the Nim Game that determines the winning strategy. This lesson helps you learn how to use modulo operations to decide if the first player can guarantee a win when both players play optimally, focusing on time and space efficient solutions.

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