Divisor Game (LeetCode 1025): Solution and Explanation

Easy
Math
Dynamic Programming
Brainteaser
Game Theory

Alice and Bob keep subtracting a proper divisor from n, and whoever cannot move loses. A small win/lose table reveals that the first player wins exactly when n is even.

Intuition

In a two-player game with perfect play, a position is winning if at least one move sends the opponent to a losing position, and losing if every move sends them to a winning one.

The only stuck position is 1, because no x satisfies 0 < x < 1. So the player facing 1 loses, and every other value can be solved from smaller ones.

Filling the table shows LOSE, WIN, LOSE, WIN... for 1, 2, 3, 4. Even numbers can always take 1 and hand over an odd number, and odd numbers only have odd divisors, so they always hand back an even number.

Approach: Bottom-up win/lose DP

  1. Create dp of size n + 1, all false. dp[1] stays false.
  2. For each i from 2 to n, try every x from 1 to i - 1.
  3. If x divides i and dp[i - x] is false, the move strands the opponent on a losing number: set dp[i] = true and stop.
  4. If no such move exists, dp[i] stays false.
  5. Return dp[n]. Once the pattern is clear, n % 2 == 0 gives the same answer.

Step-by-step example

n = 4
  1. dp[1] = false: there is no legal move from 1.
  2. i = 2: x = 1 divides 2 and leaves 1, a losing number. dp[2] = true.
  3. i = 3: the only legal move is x = 1, which leaves 2, a winning number for the opponent. dp[3] = false.
  4. i = 4: x = 1 leaves 3, a losing number. dp[4] = true.
  5. dp[4] is true, so Alice wins.

Code: Python, Java, C++

1class Solution:
2    def divisorGame(self, n: int) -> bool:
3        dp = [False] * (n + 1)
4        for i in range(2, n + 1):
5            for x in range(1, i):
6                if i % x == 0 and not dp[i - x]:
7                    dp[i] = True
8                    break
9        return dp[n]
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n^2)

Each of the n positions tries up to n moves. With n at most 1000 that is fast; the parity shortcut is O(1).

Space

O(n)

The dp table has one entry per number from 0 to n.

Other approaches

Parity (time O(1), space O(1))

Return n % 2 == 0. Even n: take 1 and the opponent faces an odd number. Odd n: every divisor is odd, so the opponent always gets an even number. Eventually the player on odd reaches 1 and loses.

Memoized recursion (time O(n^2), space O(n))

win(k) = any(k % x == 0 and not win(k - x)). Same logic top-down; the recursion depth can reach n.

Common mistakes

  • Allowing x = k. The move must satisfy 0 < x < k, otherwise the board could reach 0.
  • Treating dp[k] as 'Alice wins from k'. It means the player whose turn it is wins, which flips between turns.
  • Stopping the search at the first legal move instead of the first move into a losing position.

Frequently asked questions

Why does an even number always win?

Take x = 1, which always divides. The opponent now holds an odd number. Every divisor of an odd number is odd, and odd minus odd is even, so you get an even number back every turn. You are never stuck, because you never face 1.

Is the DP needed if parity works?

Not for the answer. The table is how you discover and convince yourself of the parity rule, and the same win/lose method solves games with no clean pattern.

What does playing optimally mean here?

Each player picks a move that leads to a losing position for the other whenever one exists. The dp recurrence encodes exactly that choice.