Find the player who will win by choosing a number in range [1, K] with sum total N
Last Updated :
23 Jul, 2025
Given two integers k and n. Two players are playing a game with these two numbers, Player1 and Player2. Player1 and Player2 take turns with Player1 starting first. At each turn, a player can choose a number in the range 1 to k, both inclusive and subtract the chosen value from n. The game ends when the value of n becomes less than or equal to 0 (n <= 0) and the player who made the last move wins the game. The task is to find the winner of the game, given that both the players play optimally and alternatively.
Examples:
Input: k = 7, n = 8
Output: Player2
Explanation: There is no way Player1 can win this game because whatever the number Player1 chooses between 1 and k, the remaining value of n for Player2 will be between 1 and k, and Player2 can choose the remaining value of n to win the game.
Input: k = 7, n = 50
Output: Player1
Explanation: From the above given example, it can be observed that if Player1 wants to win the game, the remaining value n after Player1's second last turn should be 8. Similarly, to reach 8, the remaining value n after Player1's third last turn should be 16. Here, we can observe a pattern, that if Player1 wants to win the game, the remaining value n after Player1's turn should be multiple of 8. Thus, in the first turn, Player1 can choose value 2, that makes the remaining value of n equals to 48, which is a multiple of 8, and repeating the same for all of the remaining turns, Player1 can win the game.
Input: k = 9, n = 61
Output: Player1
Explanation: If Player1 wants to win the game, the remaining value n after Player1's second last turn should be 10 (it can be validated by operating similar to second example). Thus, if Player1 wants to win the game, the remaining value n after Player1's turn should be multiple of 10. In the first turn, Player1 can choose value 1 and can make the remaining value n equals to 60 which is multiple of 10, and thus wins the game.
Approach:
The idea is to use the concept of Game Theory to find the winning state of the game. In the second example provided above, where k = 7, a pattern is observed, that if any player wants to win the game, the remaining value n after the player's turn should be multiple of 8, i.e. the winning state in this case is, that the value of n should be multiple of 8 after the player's turn.
Similarly, in the third example, where k = 9, the winning state in this case is, that the value of n should be multiple of 10 after the player's turn. In both of the cases the winning state is equal to k+1. Thus, it can be assumed, that the winning state is, when the remaining value n after player's turn is multiple of (k+1).
Therefore, the player who can make a cycle of (k+1), wins the game. Thus, at any point of game, if one player chooses value a, second player can choose (k + 1 - a) to form the cycle of (k+1) and ones the cycle is created, the other player can't broke it and lose the game.
Now, as Player1 has the first turn, Player1 should choose a number such a way that the remaining value n is multiple of (k+1), and the only possible case when Player1 can't do so is when the given value n is itself a multiple of (k+1), i.e. Player1 lose the game if n is multiple of (k+1) and wins for all other cases. Hence, if (n % (k + 1)) == 0, Player2 wins, else Player1 wins.
C++
// C++ program to find the winner
// of the numbers game.
#include <iostream>
using namespace std;
// Function to find the winner
void findTheWinner(int k, int n) {
// If n is multiple of (k+1)
// then Player2 will win
if (n % (k + 1) == 0)
cout << "Player2";
// else Player1 will win
else
cout << "Player1";
}
int main() {
int k = 7, n = 50;
findTheWinner(k, n);
return 0;
}
C
// C program to find the winner
// of the numbers game.
#include <stdio.h>
// Function to find the winner
void findTheWinner(int k, int n) {
// If n is multiple of (k+1)
// then Player2 will win
if (n % (k + 1) == 0)
printf("Player2");
// else Player1 will win
else
printf("Player1");
}
int main() {
int k = 7, n = 50;
findTheWinner(k, n);
return 0;
}
Java
// Java program to find the winner
// of the numbers game.
class GfG {
// Function to find the winner
static void findTheWinner(int k, int n) {
// If n is multiple of (k+1)
// then Player2 will win
if (n % (k + 1) == 0)
System.out.println("Player2");
// else Player1 will win
else
System.out.println("Player1");
}
public static void main(String[] args) {
int k = 7, n = 50;
findTheWinner(k, n);
}
}
Python
# Python program to find the winner
# of the numbers game.
# Function to find the winner
def findTheWinner(k, n):
# If n is multiple of (k+1)
# then Player2 will win
if n % (k + 1) == 0:
print("Player2")
# else Player1 will win
else:
print("Player1")
if __name__ == "__main__":
k = 7
n = 50
findTheWinner(k, n)
C#
// C# program to find the winner
// of the numbers game.
using System;
class GfG {
// Function to find the winner
static void FindTheWinner(int k, int n) {
// If n is multiple of (k+1)
// then Player2 will win
if (n % (k + 1) == 0)
Console.WriteLine("Player2");
// else Player1 will win
else
Console.WriteLine("Player1");
}
static void Main(string[] args) {
int k = 7, n = 50;
FindTheWinner(k, n);
}
}
JavaScript
// JavaScript program to find the winner
// of the numbers game.
// Function to find the winner
function findTheWinner(k, n) {
// If n is multiple of (k+1)
// then Player2 will win
if (n % (k + 1) === 0)
console.log("Player2");
// else Player1 will win
else
console.log("Player1");
}
const k = 7, n = 50;
findTheWinner(k, n);
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Dynamic Programming in Game Theory for Competitive Programming In the fast-paced world of competitive programming, mastering dynamic programming in game theory is the key to solving complex strategic challenges. This article explores how dynamic programming in game theory can enhance your problem-solving skills and strategic insights, giving you a competitive e
15+ min read
Optimal Strategy for a Game Given an array arr[] of size n which represents a row of n coins of values V1 . . . Vn, where n is even. We play a game against an opponent by alternating turns. In each turn, a player selects either the first or last coin from the row, removes it from the row permanently, and receives the value of
15+ min read
Optimal Strategy for a Game | Set 2 Problem statement: Consider a row of n coins of values v1 . . . vn, where n is even. We play a game against an opponent by alternating turns. In each turn, a player selects either the first or last coin from the row, removes it from the row permanently, and receives the value of the coin. Determine
15+ min read
Optimal Strategy for a Game | Set 3 Consider a row of n coins of values v1 . . . vn, where n is even. We play a game against an opponent by alternating turns. In each turn, a player selects either the first or last coin from the row, removes it from the row permanently, and receives the value of the coin. Determine the maximum possibl
15+ min read
Optimal Strategy for the Divisor game using Dynamic Programming Given an integer N and two players, A and B are playing a game. On each playerâs turn, that player makes a move by subtracting a divisor of current N (which is less than N) from current N, thus forming a new N for the next turn. The player who does not have any divisor left to subtract loses the gam
9 min read
Game of N stones where each player can remove 1, 3 or 4 Given an integer n. Two players are playing a game with pile of n stones, Player1 and Player2. Player1 and Player2 take turns with Player1 starting first. At each turn, a player can remove 1, 3 or 4 stones from the pile. The game ends when there is no stone left in the pile and the player who made t
9 min read
Find the player who will win by choosing a number in range [1, K] with sum total N Given two integers k and n. Two players are playing a game with these two numbers, Player1 and Player2. Player1 and Player2 take turns with Player1 starting first. At each turn, a player can choose a number in the range 1 to k, both inclusive and subtract the chosen value from n. The game ends when
6 min read
Find the winner of the game with N piles of boxes Given an array of strings arr[] of size n representing n piles containing white (W) and Black (B) boxes. Two players are playing a game with these piles, Player1 and Player2. Both take turns with Player1 starting first. In their respective turn, Player1 can remove any number of boxes from the top of
5 min read
Coin game of two corners (Greedy Approach) Consider a two-player coin game where each player gets turned one by one. There is a row of even a number of coins, and a player on his/her turn can pick a coin from any of the two corners of the row. The player that collects coins with more value wins the game. Develop a strategy for the player mak
9 min read