Find the player who will win the Coin game (original) (raw)

Last Updated : 27 Sep, 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:

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 // winning 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);

}

Java

// Java program to find the player // who wins the game class GFG {

// Function to check the // winning 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

Python3

Python3 program to find the player

who wins the game

Function to check the

winning 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

C#

// 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

JavaScript

`

Output:

Second Player wins the game

**Time Complexity:**O(1)

Auxiliary Space: O(1)