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’.
Input: N = 14, K = 10
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=10
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.
- 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:
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.
- 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
- 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
- Game of N stones where each player can remove 1, 3 or 4
- 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
- Find the winner of a game of removing any number of stones from the least indexed non-empty pile from given N piles
- 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
- Minimum score after flipping the cards
- Maximum possible sum after M operations on N 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 firstname.lastname@example.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.