Game of Matchsticks
Last Updated :
22 Aug, 2023
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.
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++
#include <bits/stdc++.h>
using namespace std;
int matchGame( long long N)
{
int res = N % 5;
if (res == 0) {
return -1;
}
return res;
}
int main()
{
long long int N;
N = 48;
cout << matchGame(N);
return 0;
}
|
C
#include <stdio.h>
int matchGame( long long N)
{
int res = N % 5;
if (res == 0) {
return -1;
}
return res;
}
int main()
{
long long int N;
N = 48;
printf ( "%d" , matchGame(N));
return 0;
}
|
Java
public class GFG {
static int matchGame( long N)
{
int res = ( int )(N % 5 );
if (res == 0 ) {
return - 1 ;
}
return res;
}
public static void main(String[] args)
{
long N = 48 ;
System.out.println(matchGame(N));
}
}
|
Python3
def matchGame(N):
res = N % 5
if res = = 0 :
return - 1
return res
N = 48
print (matchGame(N))
|
C#
using System;
public class GFG {
static int MatchGame( long N)
{
int res = ( int )(N % 5);
if (res == 0) {
return -1;
}
return res;
}
static void Main( string [] args)
{
long N = 48;
Console.WriteLine(MatchGame(N));
}
}
|
Javascript
function matchGame(N) {
let res = N % 5;
if (res === 0) {
return -1;
}
return res;
}
let N = 48;
console.log(matchGame(N));
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...