Given two integers N and K, where N represents the total number of cards present when game begins and K denotes the maximum number of cards that can be removed in a single turn. Two players A and B get turns to remove at most K cards, one by one starting from player A. The player to remove the last card is the winner. The task is to check if A can win the game or not. If found to be true, print ‘A’ as the answer. Otherwise, print ‘B’.
Examples:
Input: N = 14, K = 10
Output: Yes
Explanation:Â
Turn 1: A removes 3 cards in his first turn.Â
Turn 2: B removes any number of cards from the range [1 – 10]Â
Finally, A can remove all remaining cards and wins the game, as the number of remaining cards after turn 2 will be ≤ 10Input: N = 11, K=10
Output: No
Approach: The idea here is to observe that whenever the value of N % (K + 1) = 0, then A will never be able to win the game. Otherwise A always win the game.Â
Proof:
- If N ≤ K: The person who has the first turn will win the game, i.e. A.
- If N = K + 1: A can remove any number of cards in the range [1, K]. So, the total number of cards left after the first turn are also in the range [1, K]. Now B gets the turn and number of cards left are in the range [1, K]. So, B will win the game.
- If K + 2 ≤ N ≤ 2K + 1: A removes N – (K + 1) cards in his first turn. B can remove any number of cards in the range [1, K] in the next turn. Therefore, the total number of cards left now are in the range [1, K].Now, since the remaining cards left are in the range [1, K], so A can remove all the cards and win the game.
Therefore the idea is to check if N % (K + 1) is equal to 0 or not. If found to be true, print B as the winner. Otherwise, print A as the winner.
Below is the implementation of the above approach:
C++
// C++ Program to implement// the above approachÂ
#include <bits/stdc++.h>using namespace std;Â
// Function to check which// player can win the gamevoid checkWinner(int N, int K){Â Â Â Â if (N % (K + 1)) {Â Â Â Â Â Â Â Â cout << "A";Â Â Â Â }Â Â Â Â else {Â Â Â Â Â Â Â Â cout << "B";Â Â Â Â }}Â
// Driver codeint main(){Â
    int N = 50;    int K = 10;    checkWinner(N, K);} |
Java
// Java program to implement// the above approachimport java.util.*;Â
class GFG{Â
// Function to check which// player can win the gamestatic void checkWinner(int N, int K){    if (N % (K + 1) > 0)     {        System.out.print("A");    }    else    {        System.out.print("B");    }}Â
// Driver codepublic static void main(String[] args){Â Â Â Â int N = 50;Â Â Â Â int K = 10;Â Â Â Â Â Â Â Â Â checkWinner(N, K);}}Â
// This code is contributed by Amit Katiyar |
Python3
# Python3 program to implement# the above approachÂ
# Function to check which# player can win the gamedef checkWinner(N, K):Â
    if(N % (K + 1)):        print("A")    else:        print("B")Â
# Driver CodeN = 50K = 10Â
# Function callcheckWinner(N, K)Â
# This code is contributed by Shivam Singh |
C#
// C# program to implement// the above approachusing System;Â
class GFG{Â
// Function to check which// player can win the gamestatic void checkWinner(int N, int K){    if (N % (K + 1) > 0)     {        Console.Write("A");    }    else    {        Console.Write("B");    }}Â
// Driver codepublic static void Main(String[] args){Â Â Â Â int N = 50;Â Â Â Â int K = 10;Â Â Â Â Â Â Â Â Â checkWinner(N, K);}}Â
// This code is contributed by Amit Katiyar |
Javascript
<script>Â Â Â Â // Javascript Program to implement// the above approachÂ
// Function to check which// player can win the gamefunction checkWinner(N, K){Â Â Â Â if (N % (K + 1)) {Â Â Â Â Â Â Â Â document.write("A");Â Â Â Â }Â Â Â Â else {Â Â Â Â Â Â Â Â document.write("B");Â Â Â Â }}Â
// Driver codeÂ
    let N = 50;    let K = 10;    checkWinner(N, K);Â
// This code is contributed by Saurabh Jaiswal</script> |
A
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Find More here to that Topic: geeksforgeeks.org/find-the-player-who-wins-the-game-by-removing-the-last-of-given-n-cards/ […]