# Find the player who will win the Coin game

Given **N** coins, the task is to find who win the coin game.

Coin game is a game in which each player picks coins from the given **N** coins in such a way that he can pick coins ranging from 1 to 5 coins in one turn and the game continues for both the players. The player who picks the last coin loses the game.

**Examples:**

Input:N = 4Output:First PlayerExplanation:Player 1 pick 3 coins and Player 2 pick last coinInput:N = 7Output:Second Player

**Approach:**

- As the player can take coins ranging from 1 to 5 inclusively and if a player loses it means that he had only 1 coin, otherwise, he could have taken 1 less coin than available coins and force another player to lose. So now we will consider the case when the second player is going to win, which means the first player had only one coin.
- For N = 1, second player is going to win, for N = 2 to 6, first player can choose 1 less coin than N, and force the second player to lose so discard them, for N = 7, first player can choose coin 1 to 5, that’s going to leave coin ranging from 6 to 2, and then second player can choose 1 to 5, and to win second player will intelligently choose 1 less coin forcing first to loose, So basically starting from 1, all the numbers on a gap of 6(as whatever first player choose, second player will choose coins equal to difference of 6 and coins chosen by the first player) will be the winning for second player.
- Finally, we just have to check if n is of form 6*c+1 if it is then the second player going to win, otherwise, the first player going to win.

Below is the implementation for the above approach:

## C++

`// C++ program to find the player ` `// who wins the game ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check the ` `// wining player ` `void` `findWinner(` `int` `n) ` `{ ` ` ` `// As discussed in the ` ` ` `// above approach ` ` ` `if` `((n - 1) % 6 == 0) { ` ` ` `cout << ` `"Second Player wins the game"` `; ` ` ` `} ` ` ` `else` `{ ` ` ` `cout << ` `"First Player wins the game"` `; ` ` ` `} ` `} ` ` ` `// Driver function ` `int` `main() ` `{ ` ` ` ` ` `int` `n = 7; ` ` ` `findWinner(n); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the player ` `// who wins the game ` `class` `GFG ` `{ ` ` ` `// Function to check the ` `// wining player ` `static` `void` `findWinner(` `int` `n) ` `{ ` ` ` `// As discussed in the ` ` ` `// above approach ` ` ` `if` `((n - ` `1` `) % ` `6` `== ` `0` `) ` ` ` `{ ` ` ` `System.out.println(` `"Second Player wins the game"` `); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `System.out.println(` `"First Player wins the game"` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `7` `; ` ` ` `findWinner(n); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the player ` `# who wins the game ` ` ` `# Function to check the ` `# wining player ` `def` `findWinner(n): ` ` ` ` ` `# As discussed in the ` ` ` `# above approach ` ` ` `if` `((n ` `-` `1` `) ` `%` `6` `=` `=` `0` `): ` ` ` `print` `(` `"Second Player wins the game"` `); ` ` ` `else` `: ` ` ` `print` `(` `"First Player wins the game"` `); ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `7` `; ` ` ` `findWinner(n); ` ` ` `# This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the player ` `// who wins the game ` ` ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to check the ` ` ` `// wining player ` ` ` `static` `void` `findWinner(` `int` `n) ` ` ` `{ ` ` ` `// As discussed in the ` ` ` `// above approach ` ` ` `if` `((n - 1) % 6 == 0) ` ` ` `{ ` ` ` `Console.WriteLine(` `"Second Player wins the game"` `); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `Console.WriteLine(` `"First Player wins the game"` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 7; ` ` ` `findWinner(n); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output: **

Second Player wins the game

**Time Complexity:**

## Recommended Posts:

- Game of N stones where each player can remove 1, 3 or 4
- Predict the winner in Coin Game
- Coin game of two corners (Greedy Approach)
- Optimal Strategy for a Game | Special Gold Coin
- Find the minimum possible health of the winning player
- Make a fair coin from a biased coin
- Find probability that a player wins when probabilities of hitting the target are given
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Game Theory (Normal form game) | Set 2 (Game with Pure Strategy)
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Find the winner of the Game
- Find the winner in nim-game
- Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)

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.