Skip to content

Commit 7d3476c

Browse files
committed
Added Matchstick-games
1 parent e573f82 commit 7d3476c

File tree

1 file changed

+181
-0
lines changed

1 file changed

+181
-0
lines changed
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
---
2+
3+
id: matchsticks-game
4+
title: Matchsticks Game
5+
sidebar_label: 0001-Matchsticks Game
6+
tags:
7+
- Game Theory
8+
- Mathematics
9+
- Strategy
10+
- Python
11+
- JavaScript
12+
- TypeScript
13+
- Java
14+
- C++
15+
description: "This is a solution to the problem of determining the number of matchsticks the first player should pick to guarantee a win in the matchsticks game."
16+
17+
---
18+
19+
In this page, we will solve the problem of determining the number of matchsticks the first player should pick to guarantee a win in the matchsticks game using different approaches. We will provide the implementation of the solution in JavaScript, TypeScript, Python, Java, and C++.
20+
21+
## Problem Description
22+
23+
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 matchstick wins the game. If A starts first, how many matchsticks should he pick on his first 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 first turn to guarantee a win. Note: Consider both A and B play the game optimally.
24+
25+
### Examples
26+
27+
**Example 1:**
28+
29+
```plaintext
30+
Input:
31+
N = 48
32+
Output:
33+
3
34+
Explanation:
35+
Player A is guaranteed a win if he picks 3 matchsticks first.
36+
```
37+
38+
**Example 2:**
39+
40+
```plaintext
41+
Input:
42+
N = 15
43+
Output:
44+
-1
45+
Explanation:
46+
Player A is guaranteed a loss no matter how many matches he picks at first.
47+
```
48+
49+
### Constraints
50+
51+
- `1 <= N <= 10^18`
52+
53+
---
54+
55+
## Solution for Matchsticks Game Problem
56+
57+
### Intuition and Approach
58+
59+
The problem can be solved using game theory and mathematical analysis. The key insight is to notice the pattern based on the modulo operation.
60+
61+
When A starts the game, he needs to ensure that after his turn, the number of matchsticks left is such that B cannot force a win. This means A needs to leave B with a number of matchsticks that is a multiple of 5, because if $( N \% 5 = 0 )$, B will always have the advantage. Therefore, A's winning strategy is based on the remainder when $( N )$ is divided by 5.
62+
63+
- If $( N \% 5 = 0 )$, then no matter how many matchsticks A picks (1 to 4), B can always pick a number of matchsticks to maintain the advantage and eventually win. Hence, it's impossible for A to guarantee a win, and the answer is -1.
64+
- If $( N \% 5 \neq 0 )$, A should pick $( N \% 5 )$ matchsticks to leave B with a multiple of 5 matchsticks, ensuring A's winning position.
65+
66+
67+
<Tabs>
68+
<tabItem value="Game Theory" label="Game Theory">
69+
70+
### Approach: Game Theory
71+
72+
The key observation is that if the number of matchsticks \( N \) modulo 5 is 0, then player A will lose if both players play optimally. Otherwise, player A can always pick \( N \% 5 \) matchsticks to ensure a win.
73+
74+
#### Implementation
75+
76+
```jsx live
77+
function matchGame() {
78+
const N = 48;
79+
80+
const matchGame = (N) => {
81+
return N % 5 === 0 ? -1 : N % 5;
82+
};
83+
84+
const result = matchGame(N);
85+
return (
86+
<div>
87+
<p>
88+
<b>Input:</b> N = {N}
89+
</p>
90+
<p>
91+
<b>Output:</b> {result}
92+
</p>
93+
</div>
94+
);
95+
}
96+
```
97+
98+
#### Codes in Different Languages
99+
100+
<Tabs>
101+
<TabItem value="JavaScript" label="JavaScript" default>
102+
<SolutionAuthor name="@manishh12"/>
103+
```javascript
104+
function matchGame(N) {
105+
return N % 5 === 0 ? -1 : N % 5;
106+
}
107+
108+
109+
```
110+
111+
</TabItem>
112+
<TabItem value="TypeScript" label="TypeScript">
113+
<SolutionAuthor name="@manishh12"/>
114+
```typescript
115+
function matchGame(N: number): number {
116+
return N % 5 === 0 ? -1 : N % 5;
117+
}
118+
119+
120+
```
121+
122+
</TabItem>
123+
<TabItem value="Python" label="Python">
124+
<SolutionAuthor name="@manishh12"/>
125+
```python
126+
def matchGame(N):
127+
return -1 if N % 5 == 0 else N % 5
128+
129+
```
130+
131+
</TabItem>
132+
<TabItem value="Java" label="Java">
133+
<SolutionAuthor name="@manishh12"/>
134+
```java
135+
public class Solution {
136+
public int matchGame(long N) {
137+
return N % 5 == 0 ? -1 : (int)(N % 5);
138+
}
139+
140+
141+
}
142+
```
143+
144+
</TabItem>
145+
<TabItem value="C++" label="C++">
146+
<SolutionAuthor name="@manishh12"/>
147+
```cpp
148+
#include <iostream>
149+
150+
using namespace std;
151+
152+
int matchGame(long long N) {
153+
return N % 5 == 0 ? -1 : N % 5;
154+
}
155+
156+
157+
```
158+
159+
</TabItem>
160+
</Tabs>
161+
162+
#### Complexity Analysis
163+
164+
- Time Complexity: $$O(1)$$
165+
- Space Complexity: $$O(1)$$
166+
167+
</tabItem>
168+
</Tabs>
169+
170+
:::tip
171+
The key insight is recognizing the modulo operation's role in determining the game's outcome.
172+
:::
173+
174+
## References
175+
176+
- **GeeksforGeeks Problem:** [GeeksforGeeks Problem](https://www.geeksforgeeks.org/problems/-matchsticks-game4906/0)
177+
- **GeeksforGeeks Problem:** [GeeksforGeeks solution](https://www.geeksforgeeks.org/problems/-matchsticks-game4906/0)
178+
- **Solution Author:** [Manish Kumar Gupta](https://www.geeksforgeeks.org/user/manishd5hla)
179+
180+
---
181+

0 commit comments

Comments
 (0)