Two players are playing a game starting with a **number n**. In each turn, a player can make any one of the subsequent moves:

- Divide n by any of its
**odd divisors**greater than 1. Divisors of a number include the number itself. - Subtract 1 from n if n > k where k < n.

Player 1 makes the primary move, print “yes” if player 1 wins otherwise print “no” if both play optimally. The player who is unable to make a move loses the game.**Examples:**

Input:n = 12, k = 1Output:YesExplanation:

Player 1 first move = 12 / 3 = 4

Player 2 first move = 4 – 1 = 3

Player 1 second move = 3 / 3 = 1

Player 2 second move can be done and hence he looses.Input:n = 1, k = 1Output:NoExplanation:

Player 1 first move is not possible because n = k and hence player 1 looses.

**Approach:** The idea is to analyze the problem for the following 3 cases:

- When integer
**n is odd**, player 1 can divide n by itself, since it is odd and hence n / n = 1, and player 2 loses. Note that here n = 1 is an exception. - When integer
**n is even and has no odd divisors greater than 1**then n is of the form 2^{x}. Player 1 is bound to subtract it by 1 making n odd. So if x > 1, player 2 wins. Note that for x = 1, n – 1 is equal to 1, so Player 1 wins. - When
**integer n is even and has odd divisors**, the task remains to check if n is divisible by 4 then player 1 can divide n by its largest odd factor after which n becomes of the form 2^{x}where x > 1, so again player 1 wins. - Otherwise, n must be of form
**2 * p**, where p is odd. If**p is prime**, player 1 loses since he can either reduce n by 1 or divide it by p both of which would be losing for him. If**p is not prime**then p must be of the form p1 * p2 where p1 is prime and p2 is any odd number > 1, for which player 1 can win by dividing n by p2.

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the ` `// Largest Odd Divisor Game to ` `// check which player wins ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the ` `// Largest Odd Divisor Game to ` `// check which player wins ` `void` `findWinner(` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `cnt = 0; ` ` ` ` ` `// Check if n == 1 then ` ` ` `// player 2 will win ` ` ` `if` `(n == 1) ` ` ` `cout << ` `"No"` `<< endl; ` ` ` ` ` `// Check if n == 2 or n is odd ` ` ` `else` `if` `((n & 1) or n == 2) ` ` ` `cout << ` `"Yes"` `<< endl; ` ` ` ` ` `else` `{ ` ` ` `int` `tmp = n; ` ` ` `int` `val = 1; ` ` ` ` ` `// While n is greater than k and ` ` ` `// divisible by 2 keep ` ` ` `// incrementing tha val ` ` ` `while` `(tmp > k and tmp % 2 == 0) { ` ` ` `tmp /= 2; ` ` ` `val *= 2; ` ` ` `} ` ` ` ` ` `// Loop to find greatest ` ` ` `// odd divisor ` ` ` `for` `(` `int` `i = 3; i <= ` `sqrt` `(tmp); i++) { ` ` ` `while` `(tmp % i == 0) { ` ` ` `cnt++; ` ` ` `tmp /= i; ` ` ` `} ` ` ` `} ` ` ` `if` `(tmp > 1) ` ` ` `cnt++; ` ` ` ` ` `// Check if n is a power of 2 ` ` ` `if` `(val == n) ` ` ` `cout << ` `"No"` `<< endl; ` ` ` ` ` `else` `if` `(n / tmp == 2 and cnt == 1) ` ` ` `cout << ` `"No"` `<< endl; ` ` ` ` ` `// Check if cnt is not one ` ` ` `// then player 1 wins ` ` ` `else` ` ` `cout << ` `"Yes"` `<< endl; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `long` `long` `n = 1, k = 1; ` ` ` `findWinner(n, k); ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the ` `// Largest Odd Divisior Game to ` `// check which player wins ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to find the ` `// Largest Odd Divisior Game to ` `// check which player wins ` `public` `static` `void` `findWinner(` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `cnt = ` `0` `; ` ` ` ` ` `// Check if n == 1 then ` ` ` `// player 2 will win ` ` ` `if` `(n == ` `1` `) ` ` ` `System.out.println(` `"No"` `); ` ` ` ` ` `// Check if n == 2 or n is odd ` ` ` `else` `if` `((n & ` `1` `) != ` `0` `|| n == ` `2` `) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` ` ` `else` ` ` `{ ` ` ` `int` `tmp = n; ` ` ` `int` `val = ` `1` `; ` ` ` ` ` `// While n is greater than k and ` ` ` `// divisible by 2 keep ` ` ` `// incrementing tha val ` ` ` `while` `(tmp > k && tmp % ` `2` `== ` `0` `) ` ` ` `{ ` ` ` `tmp /= ` `2` `; ` ` ` `val *= ` `2` `; ` ` ` `} ` ` ` ` ` `// Loop to find greatest ` ` ` `// odd divisor ` ` ` `for` `(` `int` `i = ` `3` `; ` ` ` `i <= Math.sqrt(tmp); i++) ` ` ` `{ ` ` ` `while` `(tmp % i == ` `0` `) ` ` ` `{ ` ` ` `cnt++; ` ` ` `tmp /= i; ` ` ` `} ` ` ` `} ` ` ` `if` `(tmp > ` `1` `) ` ` ` `cnt++; ` ` ` ` ` `// Check if n is a power of 2 ` ` ` `if` `(val == n) ` ` ` `System.out.println(` `"No"` `); ` ` ` ` ` `else` `if` `(n / tmp == ` `2` `&& cnt == ` `1` `) ` ` ` `System.out.println(` `"No"` `); ` ` ` ` ` `// Check if cnt is not one ` ` ` `// then player 1 wins ` ` ` `else` ` ` `System.out.println(` `"Yes"` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `1` `, k = ` `1` `; ` ` ` ` ` `findWinner(n, k); ` `} ` `} ` ` ` `// This code is contributed by jrishabh99 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find ` `# the Largest Odd Divisor Game ` `# to check which player wins ` `import` `math ` ` ` `# Function to find the Largest ` `# Odd Divisor Game to check ` `# which player wins ` `def` `findWinner(n, k): ` ` ` ` ` `cnt ` `=` `0` `; ` ` ` ` ` `# Check if n == 1 then ` ` ` `# player 2 will win ` ` ` `if` `(n ` `=` `=` `1` `): ` ` ` `print` `(` `"No"` `); ` ` ` ` ` `# Check if n == 2 or n is odd ` ` ` `elif` `((n & ` `1` `) ` `or` `n ` `=` `=` `2` `): ` ` ` `print` `(` `"Yes"` `); ` ` ` ` ` `else` `: ` ` ` `tmp ` `=` `n; ` ` ` `val ` `=` `1` `; ` ` ` ` ` `# While n is greater than k and ` ` ` `# divisible by 2 keep ` ` ` `# incrementing tha val ` ` ` `while` `(tmp > k ` `and` `tmp ` `%` `2` `=` `=` `0` `): ` ` ` `tmp ` `/` `/` `=` `2` `; ` ` ` `val ` `*` `=` `2` `; ` ` ` ` ` `# Loop to find greatest ` ` ` `# odd divisor ` ` ` `for` `i ` `in` `range` `(` `3` `, ` `int` `(math.sqrt(tmp)) ` `+` `1` `): ` ` ` `while` `(tmp ` `%` `i ` `=` `=` `0` `): ` ` ` `cnt ` `+` `=` `1` `; ` ` ` `tmp ` `/` `/` `=` `i; ` ` ` ` ` `if` `(tmp > ` `1` `): ` ` ` `cnt ` `+` `=` `1` `; ` ` ` ` ` `# Check if n is a power of 2 ` ` ` `if` `(val ` `=` `=` `n): ` ` ` `print` `(` `"No"` `); ` ` ` ` ` `elif` `(n ` `/` `tmp ` `=` `=` `2` `and` `cnt ` `=` `=` `1` `): ` ` ` `print` `(` `"No"` `); ` ` ` ` ` `# Check if cnt is not one ` ` ` `# then player 1 wins ` ` ` `else` `: ` ` ` `print` `(` `"Yes"` `); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `1` `; k ` `=` `1` `; ` ` ` ` ` `findWinner(n, k); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the ` `// Largest Odd Divisior Game to ` `// check which player wins ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find the ` `// Largest Odd Divisior Game to ` `// check which player wins ` `public` `static` `void` `findWinner(` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `cnt = 0; ` ` ` ` ` `// Check if n == 1 then ` ` ` `// player 2 will win ` ` ` `if` `(n == 1) ` ` ` `Console.Write(` `"No"` `); ` ` ` ` ` `// Check if n == 2 or n is odd ` ` ` `else` `if` `((n & 1) != 0 || n == 2) ` ` ` `Console.Write(` `"Yes"` `); ` ` ` ` ` `else` ` ` `{ ` ` ` `int` `tmp = n; ` ` ` `int` `val = 1; ` ` ` ` ` `// While n is greater than k and ` ` ` `// divisible by 2 keep ` ` ` `// incrementing tha val ` ` ` `while` `(tmp > k && tmp % 2 == 0) ` ` ` `{ ` ` ` `tmp /= 2; ` ` ` `val *= 2; ` ` ` `} ` ` ` ` ` `// Loop to find greatest ` ` ` `// odd divisor ` ` ` `for` `(` `int` `i = 3; ` ` ` `i <= Math.Sqrt(tmp); i++) ` ` ` `{ ` ` ` `while` `(tmp % i == 0) ` ` ` `{ ` ` ` `cnt++; ` ` ` `tmp /= i; ` ` ` `} ` ` ` `} ` ` ` `if` `(tmp > 1) ` ` ` `cnt++; ` ` ` ` ` `// Check if n is a power of 2 ` ` ` `if` `(val == n) ` ` ` `Console.Write(` `"No"` `); ` ` ` ` ` `else` `if` `(n / tmp == 2 && cnt == 1) ` ` ` `Console.Write(` `"No"` `); ` ` ` ` ` `// Check if cnt is not one ` ` ` `// then player 1 wins ` ` ` `else` ` ` `Console.Write(` `"Yes"` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(` `string` `[] args) ` `{ ` ` ` `int` `n = 1, k = 1; ` ` ` ` ` `findWinner(n, k); ` `} ` `} ` `// This code is contributed by rutvik_56 ` |

*chevron_right*

*filter_none*

**Output:**

No

**Time Complexity:** O(sqrt(n)) **Auxiliary Space:** O(1)

## Recommended Posts:

- Number of wins for each player in a series of Rock-Paper-Scissor game
- Find the player who wins the game by removing the last of given N cards
- Two player game in which a player can remove all occurrences of a number
- Find probability that a player wins when probabilities of hitting the target are given
- Game Theory (Normal form game) | Set 2 (Game with Pure Strategy)
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Game of N stones where each player can remove 1, 3 or 4
- Find the player who will win the Coin game
- Optimal Strategy for the Divisor game using Dynamic Programming
- Check which player visits more number of Nodes
- Sum of greatest odd divisor of numbers in given range
- Find elements in a given range having at least one odd divisor
- Largest Divisor of a Number not divisible by a perfect square
- Largest Divisor for each element in an array other than 1 and the number itself
- Sum of largest divisor of numbers upto N not divisible by given prime number P
- Greatest divisor which divides all natural number in range [L, R]
- Find an integer X which is divisor of all except exactly one element in an array
- Combinatorial Game Theory | Set 2 (Game of Nim)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.