CSES Solutions - Another Game
Last Updated :
17 May, 2024
There are n heaps of coins and two players who move alternately. On each move, a player selects some of the nonempty heaps and removes one coin from each heap. The player who removes the last coin wins the game.
Your task is to find out who wins if both players play optimally.
Examples:
Input: N = 3, coins[] = {1, 2, 3}
Output: first
Explanation:
- The first player will remove one coin from pile 1 and pile 3 each, coins[] become {0, 2, 2}.
- The second player can either remove one coin from both the piles and then the first player will also remove one coin from both the piles leaving all the piles empty or the second player can remove one coin from any of the pile and then the first player will also remove one coin from the same pile leaving only one pile with 2 coins. Again, first player will win the game.
Input: N = 2, coins[] = {2, 2}
Output: second
Explanation:
- The first player can either remove one coin from both the piles and then the second player will also remove one coin each from both the piles leaving all the piles empty or the first player can remove one coin from any pile and then the second player will also remove one coin from the same pile leaving only one pile with 2 coins. Again, second player will win the game.
Approach:
According to the initial state of piles, we can have three cases:
- All piles have even number of coins: If all the piles have even number of coins, then from whichever piles the first player will remove the coins, the second player will also remove from the same piles again leaving all the piles with even number of coins. In this way, the second player will always be the one to remove the last coin(s). So, second player will always be the winner.
- Some piles have even number of coins and some have odd number of coins: In this case, the first player will remove one coin each from all the piles with odd number of coins leaving even number of coins in all the piles. Now, from whichever piles the second player chooses to remove a coin, the first player will remove coins from the same piles leaving the second player with piles with even number of coins. So, the first player will always be the winner.
- All piles have odd number of coins: If all the piles have odd number of coins, then the first player will remove coins from all the piles leaving all the piles with even number of coins. Now, again the second player will have all the piles with even number of coins making the first player the winner.
Step-by-step algorithm:
- Iterate over the piles and check if there is any pile with odd number of coins.
- If there is at least one pile with odd number of coins, then return first player as the winner.
- Else if all the piles have even number of coins, then return the second player as the winner.
Below is the implementation of the algorithm:
C++
#include <bits/stdc++.h>
using namespace std;
// Function to find the winner of the game
string solve(int n, vector<int>& coins)
{
// Iterate over all the coins and if there is any heaps
// with odd coins, return first player as the winner
for (int i = 0; i < n; i++) {
if (coins[i] & 1)
return "first";
}
// If all the heaps contain even coins, return second
// player as the winner
return "second";
}
int main()
{
// Sample Inputs
int n1 = 3;
vector<int> coins1 = { 1, 2, 3 };
cout << solve(n1, coins1) << "\n";
int n2 = 2;
vector<int> coins2 = { 2, 2 };
cout << solve(n2, coins2) << "\n";
int n3 = 4;
vector<int> coins3 = { 5, 5, 4, 5 };
cout << solve(n3, coins3) << "\n";
}
Java
import java.util.*;
public class Main {
// Function to find the winner of the game
static String solve(int n, List<Integer> coins)
{
// Iterate over all the coins and if there is any
// heaps with odd coins, return first player as the
// winner
for (int i = 0; i < n; i++) {
if ((coins.get(i) & 1) == 1)
return "first";
}
// If all the heaps contain even coins, return
// second player as the winner
return "second";
}
public static void main(String[] args)
{
// Sample Inputs
int n1 = 3;
List<Integer> coins1 = Arrays.asList(1, 2, 3);
System.out.println(solve(n1, coins1));
int n2 = 2;
List<Integer> coins2 = Arrays.asList(2, 2);
System.out.println(solve(n2, coins2));
int n3 = 4;
List<Integer> coins3 = Arrays.asList(5, 5, 4, 5);
System.out.println(solve(n3, coins3));
}
}
Python
# Function to find the winner of the game
def solve(n, coins):
# Iterate over all the coins and if there is any heaps
# with odd coins, return first player as the winner
for i in range(n):
if coins[i] % 2 != 0:
return "first"
# If all the heaps contain even coins, return second
# player as the winner
return "second"
def main():
# Sample Inputs
n1 = 3
coins1 = [1, 2, 3]
print(solve(n1, coins1))
n2 = 2
coins2 = [2, 2]
print(solve(n2, coins2))
n3 = 4
coins3 = [5, 5, 4, 5]
print(solve(n3, coins3))
if __name__ == "__main__":
main()
# This code is contributed by Ayush Mishra
JavaScript
// Function to find the winner of game
function solve(n, coins) {
for (let i = 0; i < n; i++) {
if (coins[i] & 1)
return "first";
}
// If all the heaps contain even coins
// return second player as the winner
return "second";
}
// Sample Inputs
const n1 = 3;
const coins1 = [1, 2, 3];
console.log(solve(n1, coins1));
const n2 = 2;
const coins2 = [2, 2];
console.log(solve(n2, coins2));
const n3 = 4;
const coins3 = [5, 5, 4, 5];
console.log(solve(n3, coins3));
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
CSES Problem Set Solutions In this article, we have compiled comprehensive, high-quality tutorials on the CSES Problem Set Solutions to assist you in understanding the problem set for learning algorithmic programming. What is CSES Problem Set?CSES Problem Set is a collection of competitive programming tasks hosted on the CSES
8 min read
A modified game of Nim Given an array arr[] of integers, two players A and B are playing a game where A can remove any number of non-zero elements from the array that are multiples of 3. Similarly, B can remove multiples of 5. The player who can't remove any element loses the game. The task is to find the winner of the ga
7 min read
Game of Matchsticks 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 m
6 min read
Mathematics for Competitive Programming Course By GeeksforGeeks Mathematics forms the foundation of problem-solving in Competitive Programming (CP). Mastering key mathematical concepts is crucial for approaching algorithmic challenges effectively. If you're an aspiring competitive programmer or someone who wishes to enhance your problem-solving skills, this Math
3 min read
Find the winner of the game based on the given moves Given two integers N and M. Two players A and B are playing this game, and the task is to find the winner A or B according to the given moves if both players play the game optimally and A starts first. The move of the games are as follows: The player destroys anyone from both integers and then divid
4 min read
Dynamic Programming in Game Theory for Competitive Programming In the fast-paced world of competitive programming, mastering dynamic programming in game theory is the key to solving complex strategic challenges. This article explores how dynamic programming in game theory can enhance your problem-solving skills and strategic insights, giving you a competitive e
15+ min read