Search⌘ K
AI Features

Solution: Nim Game

Understand how to solve the Nim Game by applying mathematical game theory and optimal play strategies. Explore checking divisibility by 4 to decide if you can guarantee a win, with O(1) 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 ...