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 integer x with 0 < x < n and n % x == 0
  • Replacing the number n on the chalkboard with n - x

If a player cannot make a move, they lose the game.

Return true if Alice wins the game, assuming both players play optimally.

Input & Output

Example 1 — Alice Loses (Odd)
$ Input: n = 3
Output: false
💡 Note: Alice can only choose x=1, giving Bob n=2. Bob then chooses x=1, giving Alice n=1. Alice cannot move and loses.
Example 2 — Alice Wins (Even)
$ Input: n = 4
Output: true
💡 Note: Alice chooses x=1, giving Bob n=3. Bob must choose x=1, giving Alice n=2. Alice chooses x=1, giving Bob n=1. Bob cannot move and loses.
Example 3 — Base Case
$ Input: n = 2
Output: true
💡 Note: Alice chooses x=1 (only valid divisor), giving Bob n=1. Bob has no valid moves and loses.

Constraints

  • 1 ≤ n ≤ 1000

Visualization

Tap to expand
Divisor Game: Game FlowStart: n=4Alice's TurnDivisors: 1,2n=3Bob's TurnDivisors: 1n=2Alice's TurnDivisors: 1n=1Bob's TurnNo moves!-1-1-1Alice Wins! (Started with even number)Pattern: Even → Odd → Even → Odd → ... → 1 (opponent loses)Even: Alice WinsOdd: Alice Loses
Understanding the Visualization
1
Input
Starting number n on chalkboard
2
Game Play
Players alternate choosing divisors and subtracting
3
Winner
Alice wins if n is even, loses if n is odd
Key Takeaway
🎯 Key Insight: Alice wins if and only if the starting number is even
Asked in
Google 15 Microsoft 12 Facebook 8
66.5K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen