Given an array arr[] of size N, the task is to find the winner of the game when two players play the game optimally as per the following rules:
- Player 1 starts the game.
- In each turn, a player removes an element from the array.
- Player 2 will win the game only if GCD of all the elements removed by Player 1 becomes equal to 1.
Examples:
Input: arr[] = { 2, 4, 8 }
Output: Player 1
Explanation:
Turn 1: Player 1 removes arr[0]. Therefore, GCD of elements removed by Player 1 = 2
Turn 2: Since GCD of elements removed by Player 1 not equal to 1. Therefore, Player 2 removes arr[1].
Turn 3: Player 1 removes arr[2]. Therefore, GCD of elements removed by Player 1 = GCD(2, 8) = 2
Since the GCD of elements removed by player 1 is not equal to 1, Player 1 wins the game.Input: arr[] = { 2, 1, 1, 1, 1, 1 }
Output: Player 2
Turn 1: Player 1 removes arr[0]. Therefore, GCD of elements removed by Player 1 = 2
Turn 2: Since GCD of elements removed by Player 1 not equal to 1, Player 2 removes arr[1].
Turn 3: Player 1 removes arr[2]. Therefore, GCD of elements removed by Player 1 = GCD(2, 1) = 1
Since GCD of elements removed by player 1 is 1, Player 2 wins the game.
Approach: The optimal way for Player 1 and Player 2 is to always always remove the array elements which have at least one common prime factor in most of the array elements. Follow the steps below to solve the problem:
- Initialize a Map, say mp, such that mp[i] stores the count of array elements whose prime factor is i
- Traverse the map and find the maximum value present in the Map, say maxCnt.
- If N is an odd number and maxCnt is equal to N or if N is an even number and maxCnt ≥ N – 1, then Player 1 wins the game.
- Otherwise, Player 2 wins the game.
Below is the implementation of the above approach:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to find the winner of the game by // removing array elements whose GCD is 1 void findWinnerGameRemoveGCD(int arr[], int n) { // mp[i]: Stores count of array // elements whose prime factor is i unordered_map<int, int> mp; // Traverse the array for (int i = 0; i < n; i++) { // Calculate the prime factors // of arr[i] for (int j = 2; j * j <= arr[i]; j++) { // If arr[i] is divisible by j if (arr[i] % j == 0) { // Update mp[j] mp[j]++; while (arr[i] % j == 0) { // Update arr[i] arr[i] = arr[i] / j; } } } // If arr[i] exceeds 1 if (arr[i] > 1) { mp[arr[i]]++; } } // Stores maximum value // present in the Map int maxCnt = 0; // Traverse the map for (auto i : mp) { // Update maxCnt maxCnt = max(maxCnt, i.second); } // If n is an even number if (n % 2 == 0) { // If maxCnt is greater // than or equal to n-1 if (maxCnt >= n - 1) { // Player 1 wins cout << "Player 1"; } else { // Player 2 wins cout << "Player 2"; } } else { // If maxCnt equal to n if (maxCnt == n) { // Player 1 wins cout << "Player 1"; } else { // Player 2 wins cout << "Player 2"; } } } // Driver Code int main() { int arr[] = { 2, 4, 8 }; int N = sizeof(arr) / sizeof(arr[0]); findWinnerGameRemoveGCD(arr, N); return 0; } |
Player 1
Time Complexity: (N * sqrt(X)), where X is the largest element of the array
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find the winner of the Game of removing odd or replacing even array elements
- Find the winner of a game of removing any number of stones from the least indexed non-empty pile from given N piles
- Predict the winner of a card game of removing K cards in each turn such that Bitwise AND of K and size of pile is 0
- Smallest subsequence having GCD equal to GCD of given array
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Game Theory (Normal form game) | Set 2 (Game with Pure Strategy)
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Find the winner in nim-game
- Find the winner of the Game to Win by erasing any two consecutive similar alphabets
- Find the winner of the game with N piles of boxes
- Find the winner of the Game
- Find the winner of the game based on greater number of divisors
- Predict the winner of the game | Sprague-Grundy
- Predict the winner in Coin Game
- Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
- Winner in the Rock-Paper-Scissor game using Bit manipulation
- Determine winner of the Game by arranging balls in a row
- Determine the winner of a game of deleting Characters from a String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

