Advertisement

Divisor Game - LeetCode 1025 Solution

Divisor Game - Complete Solution Guide

Divisor Game is LeetCode problem 1025, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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

Detailed Explanation

Alice and Bob are locked in a mathematical duel, starting with a number `n` on the chalkboard. The core challenge is understanding how their choices for `x` (a divisor of `n`, where `0 < x < n`) fundamentally alter the game state. Each turn, a player replaces `n` with `n - x`. The critical rule is that if a player cannot make a move (i.e., no such `x` exists), they lose. This immediately tells us that `n=1` is a losing state, as there are no divisors `x` satisfying `0 < x < 1`. The game revolves around optimal play: Alice wins if she can force Bob into a losing state, and Bob wins if he can force Alice into a losing state. For example, if `n=3`, Alice must choose `x=1` (the only valid divisor for `n=3`). The number becomes `3-1=2`. Bob now faces `n=2`. His only move is `x=1`, making the number `2-1=1`. Alice then receives `n=1` and loses. So, starting with `n=3`, Alice loses.

Solution Approach

The deceptively simple solution `return n % 2 == 0` reveals that the game's outcome hinges entirely on the *parity* of the initial number `n`. The key insight is that a player can always control the parity of the number they pass to their opponent if they start with an even number. If `n` is even (and `n > 1`), Alice can always choose `x=1`. Since `1` is a divisor of any integer and `0 < 1 < n` is true for `n > 1`, this is a valid move. By choosing `x=1`, Alice transforms `n` into `n-1`, which is an odd number. Conversely, if `n` is odd, any of its divisors `x` must also be odd. Therefore, `n-x` will always be an even number (odd - odd = even). This means a player who receives an odd number is *forced* to pass an even number back to their opponent. Since `n=1` is the losing state (and `1` is odd), the player who can consistently pass odd numbers will eventually force their opponent to receive `n=1`. Alice, starting with an even `n`, can always use the `x=1` strategy to pass an odd number to Bob, setting him up for failure. Bob, starting with an odd number, is forced to pass an even number back to Alice, putting her in a winning position. This continues until Alice passes `1` to Bob, thus winning the game.

Step-by-Step Algorithm

  1. Step 1: Check if the input number `n` is even using the modulo operator (`n % 2`).
  2. Step 2: If `n % 2` is equal to 0 (meaning `n` is even), return `true` (Alice wins).
  3. Step 3: Otherwise (if `n` is odd), return `false` (Alice loses).

Key Insights

  • The game's outcome is solely determined by the initial number's parity: whether `n` is even or odd. This is the fundamental pattern that governs optimal play.
  • When `n` is even (and `n > 1`), choosing `x=1` is always a valid and optimal move. This action changes the number to `n-1`, an odd number, effectively forcing the opponent into a state where they must pass an even number back.
  • If `n` is odd, all its divisors `x` must also be odd. Consequently, any move `n-x` will result in an even number (odd minus odd equals even). This means the player starting with an odd `n` is unable to pass an odd number to their opponent, thus losing the strategic advantage of controlling parity.

Complexity Analysis

Time Complexity: O(1)

Space Complexity: O(1)

Topics

This problem involves: Math, Dynamic Programming, Brainteaser, Game Theory.

Companies

Asked at: Visa.