Leetcode – Divisor Game Solution

Spread the love

The Divisor Game problem is an intriguing LeetCode challenge that delves into game theory, number theory, and dynamic programming. It’s an example of those problems which seem complex initially, but when analyzed carefully, can have a beautiful and concise solution.

In this article, we will walk through the problem statement, its significance, different approaches to tackle it, and dive deep into Python implementations.

Table of Contents:

  1. Problem Statement
  2. Significance of the Problem
  3. Approaches to Solve
  4. Python Implementations
  5. Testing the Solution
  6. Conclusion

1. Problem Statement:

Two players, Alice and Bob, play a game using a number N. On each turn, a player makes a move by:

  • Choosing any x such that 0 < x < N and N % x == 0.
  • Subtracting x from N and using the result as the new N.

The player who cannot make a move (because no such x exists) loses the game.

Given the integer N, return True if Alice wins the game with the best strategy, or False if she loses.


Input: N = 2
Output: True
Explanation: Alice chooses 1, and Bob has no more moves.

2. Significance of the Problem:

The Divisor Game tests one’s analytical skills to decipher patterns from seemingly complex scenarios. Recognizing these patterns can help optimize solutions by avoiding unnecessary calculations. This problem is a testament to the beauty of math and logic in computer science.

3. Approaches to Solve:

  1. Pattern Analysis: For small values of N, try to play the game manually and see if you can identify a pattern.
  2. Dynamic Programming: We can set up a dynamic programming solution where we decide the outcome for each value leading up to N based on previous results.
  3. Mathematical Analysis: Given the rules of the game, one can derive a mathematical solution to the problem.

4. Python Implementations:

1. Pattern Analysis:

By manually playing the game for small values of N, we might observe that Alice wins for all even values and loses for odd values. This happens because any divisor of an odd number is odd, and subtracting an odd number from another odd number results in an even number. Thus, if Alice starts with an even number, she can always make a move to ensure Bob gets an odd number.

def divisorGame(N: int) -> bool:
    return N % 2 == 0

2. Dynamic Programming:

While the pattern analysis gives us a quick answer, it’s worth understanding how a dynamic programming approach might look:

def divisorGameDP(N: int) -> bool:
    if N <= 1:
        return False
    dp = [False] * (N + 1)
    for i in range(2, N + 1):
        for j in range(1, int(i ** 0.5) + 1):
            if i % j == 0 and not dp[i - j]:
                dp[i] = True
    return dp[N]

In this approach, we maintain a DP table where dp[i] is True if Alice can win with the number i. We iterate through all numbers up to N and for each number, we find its divisors. If subtracting any of its divisors leads to a losing state for the next player, Alice can win with the current number.

5. Testing the Solution:

After implementing the solution, it’s good to test for various cases:

print(divisorGame(2))  # Expected output: True
print(divisorGame(3))  # Expected output: False
print(divisorGame(4))  # Expected output: True

print(divisorGameDP(2))  # Expected output: True
print(divisorGameDP(3))  # Expected output: False
print(divisorGameDP(4))  # Expected output: True

6. Conclusion:

The Divisor Game problem is a perfect example of how mathematical analysis and pattern recognition can lead to optimized solutions. Although the dynamic programming approach gives a correct result, understanding the underlying patterns of the problem provides a much quicker and elegant solution.

Leave a Reply