Given an array arr[] of positive distinct integers, two players A and B are playing a game. At each move, a player selects two numbers x and y from the array and if |x – y| is not present in the array then the player adds this number to the array (size of the array increases by 1). The player who can’t make the move loses the game. The task is to find the winner of the game if player A always starts the game.
Examples:
Input: arr[] = {2, 3}
Output: A
After A’s move, array will be {2, 3, 1} and B can’t make any move.
Input: arr[] = {5, 6, 7}
Output: B
Approach: Observe here that at the end of the game (when there are no more moves to make), the resultant array will contain all the multiples of the gcd of the original array upto the maximum element of the original array.
For example, arr[] = {8, 10}
Since, gcd(8, 10) = 2. So the resultant array at the end of the game will contain all the multiples of 2 ? max(arr) i.e. 10.
Hence, arr[] = {2, 4, 6, 8, 10}
From the above observation, the number of moves that can be performed on the original array can be found which will determine the winner of the game, if the number of moves is even then B will be the winner of the game else A wins the game.
Number of moves can be found as, (max(arr) / gcd) – n where gcd is the gcd of the original array elements and max(arr) / gcd gives the total number of elements in the resultant array. Subtracting original count of elements from the count of elements in the resultant array will give the number of moves.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
char getWinner( int arr[], int n)
{
int gcd = arr[0];
int maxEle = arr[0];
for ( int i = 1; i < n; i++) {
gcd = __gcd(gcd, arr[i]);
maxEle = max(maxEle, arr[i]);
}
int totalMoves = (maxEle / gcd) - n;
if (totalMoves % 2 == 1)
return 'A' ;
return 'B' ;
}
int main()
{
int arr[] = { 5, 6, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << getWinner(arr, n);
return 0;
}
|
Java
class GFG
{
static int __gcd( int a, int b)
{
if (b == 0 )
return a;
return __gcd(b, a % b);
}
static char getWinner( int []arr, int n)
{
int gcd = arr[ 0 ];
int maxEle = arr[ 0 ];
for ( int i = 1 ; i < n; i++)
{
gcd = __gcd(gcd, arr[i]);
maxEle = Math.max(maxEle, arr[i]);
}
int totalMoves = (maxEle / gcd) - n;
if (totalMoves % 2 == 1 )
return 'A' ;
return 'B' ;
}
public static void main(String args[])
{
int []arr = { 5 , 6 , 7 };
int n = arr.length;
System.out.print(getWinner(arr, n));
}
}
|
Python3
from math import gcd
def getWinner(arr, n) :
__gcd = arr[ 0 ];
maxEle = arr[ 0 ];
for i in range ( 1 , n) :
__gcd = gcd(__gcd, arr[i]);
maxEle = max (maxEle, arr[i]);
totalMoves = (maxEle / __gcd) - n;
if (totalMoves % 2 = = 1 ) :
return 'A' ;
return 'B' ;
if __name__ = = "__main__" :
arr = [ 5 , 6 , 7 ];
n = len (arr)
print (getWinner(arr, n))
|
C#
using System;
class GFG
{
static int __gcd( int a, int b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
static char getWinner( int []arr, int n)
{
int gcd = arr[0];
int maxEle = arr[0];
for ( int i = 1; i < n; i++)
{
gcd = __gcd(gcd, arr[i]);
maxEle = Math.Max(maxEle, arr[i]);
}
int totalMoves = (maxEle / gcd) - n;
if (totalMoves % 2 == 1)
return 'A' ;
return 'B' ;
}
public static void Main()
{
int []arr = { 5, 6, 7 };
int n = arr.Length;
Console.Write(getWinner(arr, n));
}
}
|
PHP
<?php
function __gcd( $a , $b )
{
if ( $b == 0)
return $a ;
return __gcd( $b , $a % $b );
}
function getWinner( $arr , $n )
{
$gcd = $arr [0];
$maxEle = $arr [0];
for ( $i = 1; $i < $n ; $i ++)
{
$gcd = __gcd( $gcd , $arr [ $i ]);
$maxEle = max( $maxEle , $arr [ $i ]);
}
$totalMoves = ( $maxEle / $gcd ) - $n ;
if ( $totalMoves % 2 == 1)
return 'A' ;
return 'B' ;
}
$arr = array (5, 6, 7);
$n = sizeof( $arr );
echo getWinner( $arr , $n );
?>
|
Javascript
<script>
function __gcd(a, b)
{
if (b == 0)
return a;
return __gcd(b, a % b);
}
function getWinner(arr, n)
{
let gcd = arr[0];
let maxEle = arr[0];
for (let i = 1; i < n; i++)
{
gcd = __gcd(gcd, arr[i]);
maxEle = Math.max(maxEle, arr[i]);
}
let totalMoves = parseInt(maxEle / gcd, 10) - n;
if (totalMoves % 2 == 1)
return 'A' ;
return 'B' ;
}
let arr = [ 5, 6, 7 ];
let n = arr.length;
document.write(getWinner(arr, n));
</script>
|
Time Complexity: O(nlogn)
Auxiliary Space: O(1)