Find the winner by adding Pairwise difference of elements in the array until Possible

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 [tabby title = "Python3"]

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

B


My Personal Notes arrow_drop_up