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

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

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## PHP

**Output:**

B

## Recommended Posts:

- Absolute Difference of all pairwise consecutive elements in an array
- Maximize the median of the given array after adding K elements to the same array
- Minimum number of elements to be removed so that pairwise consecutive elements are same
- Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
- Find set of m-elements with difference of any two elements is divisible by k
- Find minimum difference between any two elements | Set 2
- Find the winner in nim-game
- Generate original array from difference between every two consecutive elements
- Find an index such that difference between product of elements before and after it is minimum
- Sum of elements in an array whose difference with the mean of another array is less than k
- Check if elements of an array can be arranged in a Circle with consecutive difference as 1
- Find the winner of the game with N piles of boxes
- Find elements of array using XOR of consecutive elements
- Number of positions such that adding K to the element is greater than sum of all other elements
- Find minimum value to assign all array elements so that array product becomes greater

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.