Find the winner of game of removing array elements having GCD equal to 1

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:



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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.




My Personal Notes arrow_drop_up


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.