Divisor Game
EASYDescription
Alice and Bob take turns playing a game, with Alice starting first.
Initially, there is a number n on the chalkboard. On each player's turn, that player makes a move consisting of:
- Choosing any
xwith0 < x < nandn % x == 0. - Replacing the number
non the chalkboard withn - x.
Also, if a player cannot make a move, they lose the game.
Return true if and only if Alice wins the game, assuming both players play optimally.
Example 1:
Input: n = 2 Output: true Explanation: Alice chooses 1, and Bob has no more moves.
Example 2:
Input: n = 3 Output: false Explanation: Alice chooses 1, Bob chooses 1, and Alice has no more moves.
Constraints:
1 <= n <= 1000
Approaches
Checkout 2 different approaches to solve Divisor Game. Click on different approaches to view the approach and algorithm in detail.
Dynamic Programming
This approach uses dynamic programming to solve the game. We determine the winner for each number i from 1 up to n. A player wins from a state i if they can make a move to a state j from which the other player is guaranteed to lose.
Algorithm
- Create a boolean DP array
dpof sizen + 1, initialized tofalse. - Loop
ifrom 2 ton. - Inside this loop, loop
xfrom 1 toi-1. - If
iis divisible byx, check the value ofdp[i - x]. - If
dp[i - x]isfalse, it means we found a move to a losing position for the opponent. Setdp[i] = trueand break the inner loop. - After the loops complete, return
dp[n].
We can solve this game theory problem using a bottom-up dynamic programming approach. The state of the game is defined by the current number n on the chalkboard.
Let dp[i] be a boolean value indicating if the current player can win starting with the number i.
dp[i] = truemeans the current player can win.dp[i] = falsemeans the current player will lose if the opponent plays optimally.
The goal is to compute dp[n].
-
Base Case: For
n = 1, there are no valid moves (0 < x < 1is impossible). So, the player whose turn it is loses.dp[1] = false. -
Transitions: For any number
i > 1, the current player wins if they can make a move to a numberi - x(wherexis a proper divisor ofi) from which the other player loses. In other words,dp[i]istrueif there exists a divisorxofisuch thatdp[i - x]isfalse.
We can build up the dp table from i = 2 to n.
class Solution {
public boolean divisorGame(int n) {
boolean[] dp = new boolean[n + 1];
// In Java, boolean arrays are initialized to false.
// So dp[1] is already false.
for (int i = 2; i <= n; i++) {
for (int x = 1; x < i; x++) {
if (i % x == 0) {
// Check if moving to i-x is a winning move.
// A move is winning if the opponent is left in a losing state.
// A losing state for the opponent is one where dp[state] is false.
if (!dp[i - x]) {
dp[i] = true;
break; // Found a winning move, no need to check further for this i.
}
}
}
}
return dp[n];
}
}
Complexity Analysis
Pros and Cons
- It's a general and intuitive approach for solving impartial games.
- Guaranteed to find the optimal solution.
- Not the most efficient solution for this specific problem.
- The
O(n^2)time complexity might be too slow for largern, but it's acceptable forn <= 1000.
Code Solutions
Checking out 4 solutions in different languages for Divisor Game. Click on different languages to view the code.
class Solution {
public
boolean divisorGame(int n) { return n % 2 == 0; }
}
Video Solution
Watch the video walkthrough for Divisor Game
Similar Questions
5 related questions you might find useful
Patterns:
Companies:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.