Open In App

Find the player who will win by choosing a number in range [1, K] with sum total N

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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);

Output
Player1

Time Complexity: O(1)
Auxiliary Space: O(1)


Next Article
Article Tags :
Practice Tags :

Similar Reads