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 = 10Output:YesExplanation:

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 ≤ 10

Input:N= 11, K=10Output: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 game ` `void` `checkWinner(` `int` `N, ` `int` `K) ` `{ ` ` ` `if` `(N % (K + 1)) { ` ` ` `cout << ` `"A"` `; ` ` ` `} ` ` ` `else` `{ ` ` ` `cout << ` `"B"` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `N = 50; ` ` ` `int` `K = 10; ` ` ` `checkWinner(N, K); ` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement ` `// the above approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to check which ` `// player can win the game ` `static` `void` `checkWinner(` `int` `N, ` `int` `K) ` `{ ` ` ` `if` `(N % (K + ` `1` `) > ` `0` `) ` ` ` `{ ` ` ` `System.out.print(` `"A"` `); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `System.out.print(` `"B"` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `50` `; ` ` ` `int` `K = ` `10` `; ` ` ` ` ` `checkWinner(N, K); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement ` `# the above approach ` ` ` `# Function to check which ` `# player can win the game ` `def` `checkWinner(N, K): ` ` ` ` ` `if` `(N ` `%` `(K ` `+` `1` `)): ` ` ` `print` `(` `"A"` `) ` ` ` `else` `: ` ` ` `print` `(` `"B"` `) ` ` ` `# Driver Code ` `N ` `=` `50` `K ` `=` `10` ` ` `# Function call ` `checkWinner(N, K) ` ` ` `# This code is contributed by Shivam Singh ` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement ` `// the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to check which ` `// player can win the game ` `static` `void` `checkWinner(` `int` `N, ` `int` `K) ` `{ ` ` ` `if` `(N % (K + 1) > 0) ` ` ` `{ ` ` ` `Console.Write(` `"A"` `); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `Console.Write(` `"B"` `); ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `N = 50; ` ` ` `int` `K = 10; ` ` ` ` ` `checkWinner(N, K); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar` |

*chevron_right*

*filter_none*

**Output:**

A

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Largest odd divisor Game to check which player wins
- Find probability that a player wins when probabilities of hitting the target are given
- Two player game in which a player can remove all occurrences of a number
- Number of cards needed build a House of Cards of a given level N
- Predict the winner of a card game of removing K cards in each turn such that Bitwise AND of K and size of pile is 0
- 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)
- Find the player who will win the Coin game
- Find the player who is the last to remove any character from the beginning of a Binary String
- Find the player to last modify a string such that even number of consonants and no vowels are left in the string
- Game of N stones where each player can remove 1, 3 or 4
- Find the winner of a game of removing any number of stones from the least indexed non-empty pile from given N piles
- Find the winner of the Game of removing odd or replacing even array elements
- Combinatorial Game Theory | Set 2 (Game of Nim)
- Game Theory (Normal - form game) | Set 1 (Introduction)
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)
- Shuffle a deck of cards

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.