KatInterview

292. Nim Game

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem describes a game of Nim with a single heap of stones. Two players take turns removing 1, 2, or 3 stones. The player who takes the last stone wins. We go first. The goal is to determine if we can guarantee a win, assuming optimal play from both sides.

Solution

Nim Game in Python

class Solution:
    def canWinNim(self, n: int) -> bool:
        # This is a classic game theory problem.
        # Let's analyze the winning/losing states.
        # n=1: Win (take 1)
        # n=2: Win (take 2)
        # n=3: Win (take 3)
        # n=4: Lose. No matter if you take 1, 2, or 3, the opponent will be left with 3, 2, or 1 stones, respectively.
        #      In each case, the opponent can take all remaining stones and win.
        # n=5: Win. Take 1 stone, leaving 4. The opponent is now in the losing position of n=4.
        # n=6: Win. Take 2 stones, leaving 4.
        # n=7: Win. Take 3 stones, leaving 4.
        # n=8: Lose. Taking 1, 2, or 3 leaves 7, 6, or 5 stones, which are all winning positions for the opponent.
        
        # The pattern is clear: you lose if the number of stones is a multiple of 4.
        # You win if the number of stones is not a multiple of 4.
        return n % 4 != 0

Nim Game in Java

class Solution {
    public boolean canWinNim(int n) {
        // The logic is based on identifying winning and losing positions.
        // A player wins if they can force the opponent into a losing position.
        // Base cases:
        // n=1, 2, 3: You can take all stones and win. These are winning positions.
        // n=4: You can take 1, 2, or 3 stones, leaving 3, 2, or 1. In each case, the opponent can take the rest and win. So, n=4 is a losing position.
        
        // Generalization:
        // If n is a multiple of 4, any move (taking 1, 2, or 3) will leave a number of stones that is not a multiple of 4.
        // If n is not a multiple of 4, you can always make a move to leave a number of stones that IS a multiple of 4 for the opponent.
        // For example, if n = 4k + r (where r is 1, 2, or 3), you can take r stones, leaving 4k.
        // Since 4k is a losing position for the opponent, you will win.
        
        // Therefore, you can win if and only if n is not divisible by 4.
        return n % 4 != 0;
    }
}

Nim Game in C++

class Solution {
public:
    bool canWinNim(int n) {
        // This is a classic game theory problem based on winning/losing positions.
        // Let's analyze the states:
        // n=1, 2, 3: The first player can take all stones and win. These are winning positions.
        // n=4: The first player can take 1, 2, or 3 stones, leaving 3, 2, or 1.
        //        The second player can then take all remaining stones and win.
        //        So, n=4 is a losing position.
        // n=5: The first player can take 1 stone, leaving 4 for the opponent. Since 4 is a losing position, n=5 is a winning position.
        // n=6: Take 2 stones, leaving 4. Winning position.
        // n=7: Take 3 stones, leaving 4. Winning position.
        // n=8: Any move leaves 7, 6, or 5 stones, which are all winning positions for the opponent. So, n=8 is a losing position.
        
        // The pattern is that a position is a losing position if and only if n is a multiple of 4.
        // As the first player, we can win if the starting number of stones is NOT a multiple of 4.
        return n % 4 != 0;
    }
};

Nim Game in TypeScript

function canWinNim(n: number): boolean {
    // This problem can be solved by finding a pattern in winning and losing states.
    // A state is a "losing state" if every possible move from it leads to a "winning state" for the opponent.
    // A state is a "winning state" if there is at least one move that leads to a "losing state" for the opponent.
    
    // Let's analyze small values of n:
    // n = 1, 2, 3: You can take all stones in one turn. You win. (Winning states)
    // n = 4: You can take 1, 2, or 3 stones, leaving 3, 2, or 1. All of these are winning states for your opponent. So, n=4 is a losing state.
    
    // n = 5, 6, 7: You can take 1, 2, or 3 stones respectively to leave 4 stones for your opponent. Since 4 is a losing state for them, you win. (Winning states)
    // n = 8: Any move leaves 7, 6, or 5 stones, which are all winning states for your opponent. So, n=8 is a losing state.
    
    // The pattern reveals that you lose if and only if the number of stones `n` is a multiple of 4.
    // Therefore, you win if `n` is not a multiple of 4.
    return n % 4 !== 0;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.