Game of N stones where each player can remove 1, 3 or 4
Last Updated :
10 Feb, 2025
Given an integer n. Two players are playing a game with pile of n stones, Player1 and Player2. Player1 and Player2 take turns with Player1 starting first. At each turn, a player can remove 1, 3 or 4 stones from the pile. The game ends when there is no stone left in the pile and the player who made the last move wins the game. The task is to find the winner of the game, given that both the players play optimally and alternatively.
Note: If the pile of stones is empty, Player2 will be the winner.
Examples:
Input: n = 4
Output: Player1
Explanation: Player1 can remove all 4 stones from the pile in the first turn.
Input: n = 7
Output: Player2
Explanation: There are three possible combinations:
- Player1 removes 3 stones, leaving Player2 with 4 stones that can be removed in a single turn.
- Player1 removes 4 stones, leaving Player2 with 3 stones that can be removed in a single turn.
- Player1 removes 1 stone, then Player2 removes 4 stone, then Player1 again removes 1 stone (as no other options are available), and at last Player2 removes the last stone.
[Naive Approach] Using Tabulation - O(n) Time and O(1) Space
A player wins the game if the remaining stones are either 1, 3 or 4 as they can be removed in one move but loses the game if there is 2 stones left as the player will remove 1 stone and the other player will remove the second stone and win the game.
- If there are 5 stones, the player will remove 3 stones making the other player left with 2 stones which is a losing state.
- For 6 stones, the player will remove 4 stones making other player left with 2 stones.
- For 7 stones, the other player always wins the game because if the player removes 1 stone, the remaining stones will be 6, which is a winning state and same is true is player removes 3 or 4 stones.
It can be observed that to find whether n is a winning state or not, the results of n-1, n-3 and n-4 states are required. If any of these three are false, then current state is winning else losing.
The idea is to store the last 5 states, and check if any of the required three states is losing, if so current state will be winning and vice-versa. To do so, create an array dp[] of size 5. For each state check required three states, if any of these are 0, mark current as 1 else 0. Remove the first state of dp[] by shifting the remaining element to the left by 1 and add the current state at last.
C++
// CPP program to find winner of
// the game of N stones
#include <bits/stdc++.h>
using namespace std;
int findTheWinner(int n) {
// dp to store last 5 states
vector<int> dp = {0, 1, 0, 1, 1};
if(n<=4)
return dp[n];
// iterate for all states
for(int i = 5; i<=n; i++){
int cur;
// if any of the previous three possible
// states is losing then
// the current state is winning
if(dp[1] == 0 || dp[2] == 0 || dp[4] == 0){
cur = 1;
}
// if all the previous states are winning
// then the current state is losing
else {
cur = 0;
}
// remove the first element and
// shift the other elements
for(int j = 0; j<4; j++){
dp[j] = dp[j+1];
}
// add the current state at the end
dp[4] = cur;
}
return dp[4];
}
int main() {
int n = 7;
int ans = findTheWinner(n);
if(ans == 1)
cout << "Player1";
else
cout << "Player2";
return 0;
}
Java
// Java program to find winner of
// the game of N stones
import java.util.*;
class GfG {
static int findTheWinner(int n) {
// dp to store last 5 states
int[] dp = {0, 1, 0, 1, 1};
if (n <= 4)
return dp[n];
// iterate for all states
for (int i = 5; i <= n; i++) {
int cur;
// if any of the previous three possible
// states is losing then
// the current state is winning
if (dp[1] == 0 || dp[2] == 0 || dp[4] == 0) {
cur = 1;
}
// if all the previous states are winning
// then the current state is losing
else {
cur = 0;
}
// remove the first element and
// shift the other elements
for (int j = 0; j < 4; j++) {
dp[j] = dp[j + 1];
}
// add the current state at the end
dp[4] = cur;
}
return dp[4];
}
public static void main(String[] args) {
int n = 7;
int ans = findTheWinner(n);
if (ans == 1)
System.out.print("Player1");
else
System.out.print("Player2");
}
}
Python
# Python program to find winner of
# the game of N stones
def findTheWinner(n):
# dp to store last 5 states
dp = [0, 1, 0, 1, 1]
if n <= 4:
return dp[n]
# iterate for all states
for i in range(5, n + 1):
# if any of the previous three possible
# states is losing then
# the current state is winning
if dp[1] == 0 or dp[2] == 0 or dp[4] == 0:
cur = 1
else:
# if all the previous states are winning
# then the current state is losing
cur = 0
# remove the first element and
# shift the other elements
for j in range(4):
dp[j] = dp[j + 1]
# add the current state at the end
dp[4] = cur
return dp[4]
if __name__ == "__main__":
n = 7
ans = findTheWinner(n)
if ans == 1:
print("Player1")
else:
print("Player2")
C#
// C# program to find winner of
// the game of N stones
using System;
class GfG {
static int findTheWinner(int n) {
// dp to store last 5 states
int[] dp = {0, 1, 0, 1, 1};
if (n <= 4)
return dp[n];
// iterate for all states
for (int i = 5; i <= n; i++) {
int cur;
// if any of the previous three possible
// states is losing then
// the current state is winning
if (dp[1] == 0 || dp[2] == 0 || dp[4] == 0) {
cur = 1;
}
// if all the previous states are winning
// then the current state is losing
else {
cur = 0;
}
// remove the first element and
// shift the other elements
for (int j = 0; j < 4; j++) {
dp[j] = dp[j + 1];
}
// add the current state at the end
dp[4] = cur;
}
return dp[4];
}
static void Main(string[] args) {
int n = 7;
int ans = findTheWinner(n);
if (ans == 1)
Console.Write("Player1");
else
Console.Write("Player2");
}
}
JavaScript
// JavaScript program to find winner of
// the game of N stones
function findTheWinner(n) {
// dp to store last 5 states
let dp = [0, 1, 0, 1, 1];
if (n <= 4)
return dp[n];
// iterate for all states
for (let i = 5; i <= n; i++) {
let cur;
// if any of the previous three possible
// states is losing then
// the current state is winning
if (dp[1] === 0 || dp[2] === 0 || dp[4] === 0) {
cur = 1;
}
// if all the previous states are winning
// then the current state is losing
else {
cur = 0;
}
// remove the first element and
// shift the other elements
for (let j = 0; j < 4; j++) {
dp[j] = dp[j + 1];
}
// add the current state at the end
dp[4] = cur;
}
return dp[4];
}
let n = 7;
let ans = findTheWinner(n);
if (ans === 1)
console.log("Player1");
else
console.log("Player2");
[Expected Approach] Using Mathematics - O(1) Time and O(1) Space
If we observe carefully the results are repeating themselves after 7 numbers, where the first set is [0, 1, 0, 1, 1, 1, 1]. And the next 7 results are exactly same, thus we can simply check if the number has remainder 0 or 2 when divided by 7, if so, the Player2 will win, else the Player1 will win.
C++
// CPP program to find winner of
// the game of N stones
#include <bits/stdc++.h>
using namespace std;
int findTheWinner(int n) {
// If n is multiple of 7
// then Player 2 will win
if(n % 7 == 0 || n%7 == 2)
return 0;
// else Player 1 will win
else return 1;
}
int main() {
int n = 7;
int ans = findTheWinner(n);
if(ans == 1)
cout << "Player1";
else
cout << "Player2";
return 0;
}
Java
// Java program to find winner of
// the game of N stones
class GFG {
static int findTheWinner(int n) {
// If n is multiple of 7
// then Player 2 will win
if(n % 7 == 0 || n%7 == 2)
return 0;
// else Player 1 will win
else return 1;
}
public static void main(String[] args) {
int n = 7;
int ans = findTheWinner(n);
if(ans == 1)
System.out.print("Player1");
else
System.out.print("Player2");
}
}
Python
# Python program to find winner of
# the game of N stones
def findTheWinner(n):
# If n is multiple of 7
# then Player 2 will win
if n % 7 == 0 or n % 7 == 2:
return 0
# else Player 1 will win
else:
return 1
if __name__ == "__main__":
n = 7
ans = findTheWinner(n)
if ans == 1:
print("Player1")
else:
print("Player2")
C#
// C# program to find winner of
// the game of N stones
using System;
class GFG {
static int findTheWinner(int n) {
// If n is multiple of 7
// then Player 2 will win
if(n % 7 == 0 || n % 7 == 2)
return 0;
// else Player 1 will win
else return 1;
}
public static void Main() {
int n = 7;
int ans = findTheWinner(n);
if(ans == 1)
Console.Write("Player1");
else
Console.Write("Player2");
}
}
JavaScript
// JavaScript program to find winner of
// the game of N stones
function findTheWinner(n) {
// If n is multiple of 7
// then Player 2 will win
if(n % 7 === 0 || n % 7 === 2)
return 0;
// else Player 1 will win
else return 1;
}
// Driver code
let n = 7;
let ans = findTheWinner(n);
if(ans === 1)
console.log("Player1");
else
console.log("Player2");
Time Complexity: O(1)
Space Complexity: O(1)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem