Search⌘ K
AI Features

Solution: Nim Game

Explore the winning strategy for the Nim Game by understanding the mathematical pattern of stones and optimal play. Learn to identify when you can guarantee a win by analyzing the heap size modulo 4. This lesson helps you apply game theory and efficient problem-solving techniques to coding challenges.

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