Open In App

Number of coins to be removed for the first player to win the Game

Improve
Improve
Like Article
Like
Save
Share
Report

Two friends, A, and B are playing a game. In this game, a group of N coins is placed. The players can pick any number of coins between 1 and 4 (both inclusive) in their chance. The player who takes the last coin wins the game. If A starts first, find out how many coins should he pick such that he is guaranteed to win the game or determine if it’s impossible for him to win? 

Note: Consider both A and B to play the game optimally.

Examples:

Input: N = 18
Output: 3
Explanation: Player A is guaranteed a win if he picks 3 coins first.

Input: N = 10
Output: -1
Explanation: Player A is guaranteed to lose no matter how many matches he picks at first.

Approach: This problem can be solved by observing the fact that the sum of two numbers can always be made 5. 

Considering all cases possible:

  • If one selects 1 coin then the second can take 4 to make the sum of 5.
  • If one selects 2 coins then the second can take 3 to make the sum of 5.
  • If one selects 3 coins then the second can take 2 to make the sum of 5.
  • If one selects 4 coins then the second can take 1 to make the sum of 5.

So in all cases, the second player can make a sum of 5. Hence if Player “A” can make the total number of coins left a multiple of 5 after the first turn then in each even number of turns after that, player “A” can make that sum a multiple of (as whatever B picks the player A will be able to make the sum as 5 and hence can make any multiple of 5). And hence be available to win the game.

Follow the steps to solve the problem:

  • In order to make the coins left a multiple of 5, player A has to select N % 5 coins. 
  • And if N%5 = 0 then he cannot make a sum of 5 as he can take from 1 to 4 coins only. 
    • So he will be guaranteed to lose the game. 
  • Hence if N % 5 = 0 then he will lose the game and if it is non 0 then player A will choose N % 5 and be guaranteed to win the game.

Below is the implementation of the above approach.

C++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to findthe number of coins
void coinGame(long long n)
{
    if (n % 5 == 0) {
        cout << "Player A will lose" << endl;
    }
    else {
        cout << "Player A should pick " << n % 5
             << " coins" << endl;
    }
}
 
// Driver code
int main()
{
    // First test case
    int N = 18;
    coinGame(N);
 
    // Second test case
    N = 10;
    coinGame(N);
 
    // Third test case
    N = 3;
    coinGame(N);
 
    return 0;
}


Java




// Java code to implement the approach
 
import java.io.*;
 
class GFG {
 
    static void coinGame(int n)
    {
        if (n % 5 == 0) {
            System.out.println("Player A will lose");
        }
        else {
            System.out.println("Player A should pick "
                               + n % 5 + " coins");
        }
    }
 
    public static void main(String[] args)
    {
        // First test case
        int N = 18;
        coinGame(N);
 
        // Second test case
        N = 10;
        coinGame(N);
 
        // Third test case
        N = 3;
        coinGame(N);
    }
}
 
// This code is contributed by lokesh.


Python3




# Python code to implement the approach
def coin_game(n):
    if n % 5 == 0:
        print("Player A will lose")
    else:
        print("Player A should pick " + str(n % 5) + " coins")
 
# First test case
N = 18
coin_game(N)
 
# Second test case
N = 10
coin_game(N)
 
# Third test case
N = 3
coin_game(N)
 
# This code is contributed by lokesh.


C#




// C# code to implement the approach
using System;
public class GFG {
 
  static void coinGame(int n)
  {
    if (n % 5 == 0) {
      Console.WriteLine("Player A will lose");
    }
    else {
      Console.WriteLine("Player A should pick "
                        + n % 5 + " coins");
    }
  }
 
  static public void Main()
  {
    // First test case
    int N = 18;
    coinGame(N);
 
    // Second test case
    N = 10;
    coinGame(N);
 
    // Third test case
    N = 3;
    coinGame(N);
  }
}
 
// This code is contributed by sanjoy_62.


Javascript




// JAVASCRIPT code to implement the approach
 
// Function to find the number of coins
function coinGame( n)
{
    if (n % 5 == 0) {
        console.log("Player A will lose");
    }
    else {
        console.log("Player A should pick " + n % 5
            + " coins");
    }
}
 
// Driver code
 
    // First test case
    let N = 18;
    coinGame(N);
 
    // Second test case
    N = 10;
    coinGame(N);
 
    // Third test case
    N = 3;
    coinGame(N);
 
   // This code is contributed by garg28harsh.


Output

Player A should pick 3 coins
Player A will lose
Player A should pick 3 coins

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



Last Updated : 23 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads