Open In App

Game of Matchsticks

Last Updated : 22 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Two friends, A and B, are playing the game of matchsticks. In this game, a group of N matchsticks is placed on the table. The players can pick any number of matchsticks from 1 to 4 (both inclusive) during their chance. The player who takes the last match stick wins the game. If A starts first, how many matchsticks should he pick on his 1st turn such that he is guaranteed to win the game or determine if it’s impossible for him to win? Return -1 if it’s impossible for A to win the game, else return the number of matchsticks he should pick on his 1st turn such that he is guaranteed to win.

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

Examples:

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

Input: N = 15
Output: -1
Explanation: Player A is guaranteed a loss no matter how many matches he picks at first.

Approach: To solve the problem follow the below idea:

Firstly, when we read the problem statement, the first approach comes to our mind is of Dynamic Programming, but when we look at the constraints then this approach might not be applicable in this particular problem. Here we have to come up with some mathematical solution. Note that Always, consider modulo operation in your thinking when came up with mathematics problem in DSA. The problem can be solved only by observation. If we consider output for some value of N then we will see that there is a pattern present in this problem.

So, let’s observe some patterns for some values of N.

N

Number of Matchsticks (N%5)

Win(A/B)

1 1 A
2 2 A
3 3 A
4 4 A
5 -1 B
6 1 A
7 2 A
8 3 A
9 4 A
10 -1 B
11 1 A
12 2 A

The second column of the table represents the number of matchsticks A should initially pick so that A can win the game. From the table, it is easy to observe that the answer is nothing but the remainder after dividing N by 5. if N is a multiple of 5, then, the answer will be 1, in this case, there is no chance for A to win the game. N%5 gives the number of matchsticks A should initially pick so that A can win the game.

Follow the steps below to implement the above idea:

  • If N is not divisible by 5 then the answer will be the remainder after dividing N by 5. N/5 matchsticks A should pick initially.
  • If N is a multiple of 5 then there is no possibility of A to win the game then we return -1.

Below is the implementation of the above approach:

C++




// C++ program for the above approach:
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate number
// of match stick A should pick initially
int matchGame(long long N)
{
 
    // res variable store the
    // number of matchsticks
    // initially A pick
    int res = N % 5;
 
    // If res or N%5 is 0 then there
    // is no chance of A to win the game
    if (res == 0) {
        return -1;
    }
 
    // else return the remainder
    // value after dividing N by 5
    return res;
}
 
// Driver code
int main()
{
 
    // Total number of Matchstics is N
    long long int N;
 
    N = 48;
 
    // Function call
    cout << matchGame(N);
    return 0;
}


C




// Include Header files
#include <stdio.h>
 
//   Function to calculate number
//   of match stick A should
// pick initially
int matchGame(long long N)
{
    // res variable store the
    // number of matchsticks
    // initially A pick
    int res = N % 5;
 
    // If res or N%5 is 0 then
    // there is no chance of A to
    // win the game
    if (res == 0) {
        return -1;
    }
 
    // else return the remainder
    // value after dividing N by 5
    return res;
}
 
// Driver code
int main()
{
    // Total number of Matchstics is N
    long long int N;
 
    N = 48;
 
    printf("%d", matchGame(N));
    return 0;
}


Java




// Java program for the above approach
 
public class GFG {
    // Function to calculate the number of matchsticks A
    // should pick initially
    static int matchGame(long N)
    {
        // res variable stores the number of matchsticks
        // initially picked by A
        int res = (int)(N % 5);
 
        // If res or N%5 is 0 then there is no chance of A
        // to win the game
        if (res == 0) {
            return -1;
        }
 
        // else return the remainder value after dividing N
        // by 5
        return res;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // Total number of matchsticks is N
        long N = 48;
 
        // Function call
        System.out.println(matchGame(N));
    }
}
 
// This code is contributed by Susobhan Akhuli


Python3




# Python program for the above approach
def matchGame(N):
    # res variable store the number of matchsticks initially A pick
    res = N % 5
 
    # If res or N%5 is 0 then there is no chance of A to win the game
    if res == 0:
        return -1
 
    # else return the remainder value after dividing N by 5
    return res
 
# Driver code
N = 48
 
# Function call
print(matchGame(N))
 
# This code is contributed by Susobhan Akhuli


C#




// C# program for the above approach
using System;
 
public class GFG {
    // Function to calculate number
    // of match stick A should pick initially
    static int MatchGame(long N)
    {
        // res variable store the
        // number of matchsticks
        // initially A pick
        int res = (int)(N % 5);
 
        // If res or N%5 is 0 then there
        // is no chance of A to win the game
        if (res == 0) {
            return -1;
        }
 
        // else return the remainder
        // value after dividing N by 5
        return res;
    }
 
    // Driver code
    static void Main(string[] args)
    {
        // Total number of Matchsticks is N
        long N = 48;
 
        // Function call
        Console.WriteLine(MatchGame(N));
    }
}
 
// This code is contributed by Susobhan Akhuli


Javascript




// JavaScript program for the above approach
 
// Function to calculate number
// of match stick A should pick initially
function matchGame(N) {
    // res variable store the
    // number of matchsticks
    // initially A pick
    let res = N % 5;
 
    // If res or N%5 is 0 then there
    // is no chance of A to win the game
    if (res === 0) {
        return -1;
    }
 
    // else return the remainder
    // value after dividing N by 5
    return res;
}
 
// Driver code
 
// Total number of Matchsticks is N
let N = 48;
 
// Function call
console.log(matchGame(N));
 
// This code is contributed by Susobhan Akhuli


Output

3

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads