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 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++
#include <bits/stdc++.h>
using namespace std;
void findWinnerGameRemoveGCD( int arr[], int n)
{
unordered_map< int , int > mp;
for ( int i = 0; i < n; i++) {
for ( int j = 2; j * j <= arr[i];
j++) {
if (arr[i] % j == 0) {
mp[j]++;
while (arr[i] % j == 0) {
arr[i] = arr[i] / j;
}
}
}
if (arr[i] > 1) {
mp[arr[i]]++;
}
}
int maxCnt = 0;
for ( auto i : mp) {
maxCnt = max(maxCnt,
i.second);
}
if (n % 2 == 0) {
if (maxCnt >= n - 1) {
cout << "Player 1" ;
}
else {
cout << "Player 2" ;
}
}
else {
if (maxCnt == n) {
cout << "Player 1" ;
}
else {
cout << "Player 2" ;
}
}
}
int main()
{
int arr[] = { 2, 4, 8 };
int N = sizeof (arr)
/ sizeof (arr[0]);
findWinnerGameRemoveGCD(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findWinnerGameRemoveGCD( int arr[], int n)
{
HashMap<Integer,
Integer> mp = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < n; i++) {
for ( int j = 2 ; j * j <= arr[i];
j++) {
if (arr[i] % j == 0 ) {
if (mp.containsKey(j))
{
mp.put(j, mp.get(j) + 1 );
}
else
{
mp.put(j, 1 );
}
while (arr[i] % j == 0 ) {
arr[i] = arr[i] / j;
}
}
}
if (arr[i] > 1 ) {
if (mp.containsKey(arr[i]))
{
mp.put(arr[i], mp.get(arr[i]) + 1 );
}
else
{
mp.put(arr[i], 1 );
}
}
}
int maxCnt = 0 ;
for (Map.Entry<Integer,
Integer> i : mp.entrySet()) {
maxCnt = Math.max(maxCnt, i.getValue());
}
if (n % 2 == 0 ) {
if (maxCnt >= n - 1 ) {
System.out.print( "Player 1" );
}
else {
System.out.print( "Player 2" );
}
}
else {
if (maxCnt == n) {
System.out.print( "Player 1" );
}
else {
System.out.print( "Player 2" );
}
}
}
public static void main(String[] args)
{
int arr[] = { 2 , 4 , 8 };
int N = arr.length;
findWinnerGameRemoveGCD(arr, N);
}
}
|
Python3
def findWinnerGameRemoveGCD(arr, n) :
mp = dict .fromkeys(arr, 0 );
for i in range (n) :
for j in range ( 2 , int (arr[i] * ( 1 / 2 )) + 1 ) :
if (arr[i] % j = = 0 ) :
mp[j] + = 1 ;
while (arr[i] % j = = 0 ) :
arr[i] = arr[i] / / j;
if (arr[i] > 1 ) :
mp[arr[i]] + = 1 ;
maxCnt = 0 ;
for i in mp:
maxCnt = max (maxCnt,mp[i]);
if (n % 2 = = 0 ) :
if (maxCnt > = n - 1 ) :
print ( "Player 1" ,end = "");
else :
print ( "Player 2" ,end = "");
else :
if (maxCnt = = n) :
print ( "Player 1" ,end = "");
else :
print ( "Player 2" ,end = "");
if __name__ = = "__main__" :
arr = [ 2 , 4 , 8 ];
N = len (arr);
findWinnerGameRemoveGCD(arr, N);
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void findWinnerGameRemoveGCD( int []arr, int n)
{
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0; i < n; i++) {
for ( int j = 2; j * j <= arr[i];
j++) {
if (arr[i] % j == 0)
{
if (mp.ContainsKey(j))
{
mp[j] = mp[j] + 1;
}
else
{
mp.Add(j, 1);
}
while (arr[i] % j == 0)
{
arr[i] = arr[i] / j;
}
}
}
if (arr[i] > 1)
{
if (mp.ContainsKey(arr[i]))
{
mp[arr[i]] = mp[arr[i]] + 1;
}
else
{
mp.Add(arr[i], 1);
}
}
}
int maxCnt = 0;
foreach (KeyValuePair< int ,
int > i in mp)
{
maxCnt = Math.Max(maxCnt, i.Value);
}
if (n % 2 == 0)
{
if (maxCnt >= n - 1)
{
Console.Write( "Player 1" );
}
else
{
Console.Write( "Player 2" );
}
}
else
{
if (maxCnt == n)
{
Console.Write( "Player 1" );
}
else
{
Console.Write( "Player 2" );
}
}
}
public static void Main(String[] args)
{
int []arr = { 2, 4, 8 };
int N = arr.Length;
findWinnerGameRemoveGCD(arr, N);
}
}
|
Javascript
<script>
function findWinnerGameRemoveGCD(arr, n)
{
let mp = new Map();
for (let i = 0; i < n; i++) {
for (let j = 2; j * j <= arr[i];j++) {
if (arr[i] % j == 0) {
if (mp.has(j)){
mp.set(j,mp.get(j)+1);
}
else mp.set(j,1);
while (arr[i] % j == 0) {
arr[i] = Math.floor(arr[i] / j);
}
}
}
if (arr[i] > 1) {
if (mp.has(arr[i])){
mp.set(arr[i],mp.get(arr[i])+1);
}
else mp.set(arr[i],1);
}
}
let maxCnt = 0;
for (let [i,j] of mp) {
maxCnt = Math.max(maxCnt,j);
}
if (n % 2 == 0) {
if (maxCnt >= n - 1) {
document.write( "Player 1" );
}
else {
document.write( "Player 2" );
}
}
else {
if (maxCnt == n) {
document.write( "Player 1" );
}
else {
document.write( "Player 2" );
}
}
}
let arr = [ 2, 4, 8 ];
let N = arr.length;
findWinnerGameRemoveGCD(arr, N);
</script>
|
Time Complexity: (N * sqrt(X)), where X is the largest element of the array
Auxiliary Space: O(N)
Last Updated :
01 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...