Skip to content
Related Articles

Related Articles

Improve Article
Find the winner by adding Pairwise difference of elements in the array until Possible
  • Last Updated : 02 Jun, 2021

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:
After A’s move, array will be {2, 3, 1} and B can’t make any move. 
Input: arr[] = {5, 6, 7} 
Output:
 

 

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++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the winner of the game
char getWinner(int arr[], int n)
{
    // To store the gcd of the original array
    int gcd = arr[0];
 
    // To store the maximum element
    // from the original array
    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 number of moves are odd
    if (totalMoves % 2 == 1)
        return 'A';
 
    return 'B';
}
 
// Driver Code
int main()
{
    int arr[] = { 5, 6, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << getWinner(arr, n);
 
    return 0;
}

Java




// Java implementation of the approach
class GFG
{
     
// Function to calculate gcd
static int __gcd(int a, int b)
{
    if (b == 0)
        return a;
    return __gcd(b, a % b);
}
 
// Function to return the winner
// of the game
static char getWinner(int []arr, int n)
{
    // To store the gcd of the
    // original array
    int gcd = arr[0];
 
    // To store the maximum element
    // from the original array
    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 number of moves are odd
    if (totalMoves % 2 == 1)
        return 'A';
 
    return 'B';
}
 
// Driver Code
public static void main(String args[])
{
    int []arr = { 5, 6, 7 };
    int n = arr.length;
    System.out.print(getWinner(arr, n));
}
}
 
// This code is contributed
// by Akanksha Rai

Python3




# Python3 implementation of the approach
from math import gcd
 
# Function to return the winner
# of the game
def getWinner(arr, n) :
     
    # To store the gcd of the
    # original array
    __gcd = arr[0];
 
    # To store the maximum element
    # from the original array
    maxEle = arr[0];
    for i in range(1, n) :
        __gcd = gcd(__gcd, arr[i]);
        maxEle = max(maxEle, arr[i]);
     
    totalMoves = (maxEle / __gcd) - n;
 
    # If number of moves are odd
    if (totalMoves % 2 == 1) :
        return 'A';
 
    return 'B';
 
# Driver Code
if __name__ == "__main__" :
     
    arr = [ 5, 6, 7 ];
    n = len(arr)
    print(getWinner(arr, n))
     
# This code is contributed by Ryuga

C#




// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to calculate gcd
static int __gcd(int a, int b)
{    
    if (b == 0)
        return a;
    return __gcd(b, a % b);
}
 
// Function to return the winner
// of the game
static char getWinner(int []arr, int n)
{
    // To store the gcd of the
    // original array
    int gcd = arr[0];
 
    // To store the maximum element
    // from the original array
    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 number of moves are odd
    if (totalMoves % 2 == 1)
        return 'A';
 
    return 'B';
}
 
// Driver Code
public static void Main()
{
    int []arr = { 5, 6, 7 };
    int n = arr.Length;
    Console.Write(getWinner(arr, n));
}
}
 
// This code is contributed
// by Akanksha Rai

PHP




<?php
// PHP implementation of the approach
 
// Function to calculate gcd
function __gcd($a, $b)
{
    if ($b == 0)
        return $a;
    return __gcd($b, $a % $b);
}
 
// Function to return the winner
// of the game
function getWinner($arr, $n)
{
    // To store the gcd of the
    // original array
    $gcd = $arr[0];
 
    // To store the maximum element
    // from the original array
    $maxEle = $arr[0];
    for ($i = 1; $i < $n; $i++)
    {
        $gcd = __gcd($gcd, $arr[$i]);
        $maxEle = max($maxEle, $arr[$i]);
    }
 
    $totalMoves = ($maxEle / $gcd) - $n;
 
    // If number of moves are odd
    if ($totalMoves % 2 == 1)
        return 'A';
 
    return 'B';
}
 
// Driver Code
$arr = array(5, 6, 7);
$n = sizeof($arr);
echo getWinner($arr, $n);
 
// This code is contributed
// by Akanksha Rai
?>

Javascript




<script>
 
    // JavaScript implementation of the approach
     
    // Function to calculate gcd
    function __gcd(a, b)
    {    
        if (b == 0)
            return a;
        return __gcd(b, a % b);
    }
 
    // Function to return the winner
    // of the game
    function getWinner(arr, n)
    {
        // To store the gcd of the
        // original array
        let gcd = arr[0];
 
        // To store the maximum element
        // from the original array
        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 number of moves are odd
        if (totalMoves % 2 == 1)
            return 'A';
 
        return 'B';
    }
     
    let arr = [ 5, 6, 7 ];
    let n = arr.length;
    document.write(getWinner(arr, n));
     
</script>
Output: 
B

 

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live




My Personal Notes arrow_drop_up
Recommended Articles
Page :