Find the player who will win the Coin game - GeeksforGeeks

06 Apr, 2022
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 = 4
Output: First Player
Explanation:
Player 1 pick 3 coins and 
Player 2 pick last coin

Input: N = 7
Output: 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++
Java
Python3
C#
Javascript
// C++ program to find the player// who wins the game #include <bits/stdc++.h>using namespace std; // Function to check the// winning playervoid 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 functionint main(){     int n = 7;    findWinner(n);}
// Java program to find the player// who wins the gameclass GFG{ // Function to check the// winning playerstatic 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 Codepublic static void main(String[] args){    int n = 7;    findWinner(n);}} // This code is contributed by Rajput-Ji
# Python3 program to find the player# who wins the game # Function to check the# winning playerdef 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 Codeif __name__ == '__main__':    n = 7;    findWinner(n); # This code is contributed by 29AjayKumar
// C# program to find the player// who wins the game using System; class GFG{         // Function to check the    // winning 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
<script> // JavaScript program to find the player// who wins the game // Function to check the// winning playerfunction findWinner(n){    // As discussed in the    // above approach    if ((n - 1) % 6 == 0) {        document.write("Second Player wins the game");    }    else {        document.write("First Player wins the game");    }} // Driver functionvar n = 7;findWinner(n); </script>
Output: 
Second Player wins the game
Time Complexity:
Auxiliary Space: O(1) 
Rajput-Ji
ankthon
29AjayKumar
rutvik_56
subham348
simmytarika5
number-theory
Game Theory
Mathematical
number-theory
Mathematical
Game Theory
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Classification of Algorithms with Examples
A Binary String Game
Find the winner of a game of removing any number of stones from the least indexed non-empty pile from given N piles
Minimum number of moves to make M and N equal by repeatedly adding any divisor of number to itself except 1 and the number
Find the winner of game of repeatedly removing the first character to empty given string
Program for Fibonacci numbers
C++ Data Types
Write a program to print all permutations of a given string
Set in C++ Standard Template Library (STL)
Coin Change | DP-7