# Game of replacing array elements

There are two players A and B who are interested in playing a game of numbers. In each move a player pick two distinct number, let’s say *a1* and *a2* and then replace all *a2* by *a1* or *a1* by *a2*. They stop playing game if any one of them is unable to pick two number and the player who is unable to pick two distinct number in an array, looses the game. First player always move first and then second. Task is to find which player wins.

Examples:

Input : arr[] = { 1, 3, 3, 2,, 2, 1 } Output : Player 2 wins Explanation: First plays always looses irrespective of the numbers chosen by him. For example, say first player picks ( 1 & 3) replace all 3 by 1 Now array Become { 1, 1, 1, 2, 2, 1 } Then second player picks ( 1 2 ) either he replace 1 by 2 or 2 by 1 Array Become { 1, 1, 1, 1, 1, 1 } Now first player is not able to choose. Input : arr[] = { 1, 2, 1, 2 } Output : Player 1 wins

From above examples, we can observe that if number of count of distinct element is even, first player always wins. Else second player wins.

Lets take an another example :

int arr[] = 1, 2, 3, 4, 5, 6

Here number of distinct element is even(n). If player 1 pick any two number lets say (4, 1), then we left with n-1 distinct element. So player second left with n-1 distinct element. This precess go on until distinct element become 1. Here **n = 6**

Player : P1 p2 P1 p2 P1 P2 distinct : [n, n-1, n-2, n-3, n-4, n-5 ] "At this point no distinct element left, so p2 is unable to pick two Dis element."

Below implementation of above idea :

## C++

`// CPP program for Game of Replacement ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function return which player win the game ` `int` `playGame(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Create hash that will stores ` ` ` `// all distinct element ` ` ` `unordered_set<` `int` `> hash; ` ` ` ` ` `// Traverse an array element ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `hash.insert(arr[i]); ` ` ` ` ` `return` `(hash.size() % 2 == 0 ? 1 : 2); ` `} ` ` ` `// Driver Function ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 1, 2, 2, 2, 2 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << ` `"Player "` `<< playGame(arr, n) << ` `" Wins"` `<< endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for Game of Replacement ` `import` `java.util.HashSet; ` `public` `class` `GameOfReplacingArrayElements ` `{ ` ` ` `// Function return which player win the game ` ` ` `public` `static` `int` `playGame(` `int` `arr[]) ` ` ` `{ ` ` ` `// Create hash that will stores ` ` ` `// all distinct element ` ` ` `HashSet<Integer> set=` `new` `HashSet<>(); ` ` ` ` ` `// Traverse an array element ` ` ` `for` `(` `int` `i:arr) ` ` ` `set.add(i); ` ` ` `return` `(set.size()%` `2` `==` `0` `)?` `1` `:` `2` `; ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String args[]) { ` ` ` `int` `arr[] = { ` `1` `, ` `1` `, ` `2` `, ` `2` `, ` `2` `, ` `2` `}; ` ` ` `System.out.print(` `"Player "` `+playGame(arr)+` `" wins"` `); ` ` ` `} ` `} ` `//This code is contributed by Gaurav Tiwari ` |

*chevron_right*

*filter_none*

## Python3

`# Python program for Game of Replacement ` ` ` `# Function return which player win the game ` `def` `playGame(arr, n): ` ` ` ` ` `# Create hash that will stores ` ` ` `# all distinct element ` ` ` `s ` `=` `set` `() ` ` ` ` ` `# Traverse an array element ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `s.add(arr[i]) ` ` ` `return` `1` `if` `len` `(s) ` `%` `2` `=` `=` `0` `else` `2` ` ` `# Driver code ` `arr ` `=` `[` `1` `, ` `1` `, ` `2` `, ` `2` `, ` `2` `, ` `2` `] ` `n ` `=` `len` `(arr) ` `print` `(` `"Player"` `,playGame(arr, n),` `"Wins"` `) ` ` ` `# This code is contributed by Shrikant13 ` |

*chevron_right*

*filter_none*

## C#

`// C# program for Game of Replacement ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `public` `class` `GameOfReplacingArrayElements ` `{ ` ` ` `// Function return which player win the game ` ` ` `public` `static` `int` `playGame(` `int` `[]arr) ` ` ` `{ ` ` ` `// Create hash that will stores ` ` ` `// all distinct element ` ` ` `HashSet<` `int` `> ` `set` `= ` `new` `HashSet<` `int` `>(); ` ` ` ` ` `// Traverse an array element ` ` ` `foreach` `(` `int` `i ` `in` `arr) ` ` ` `set` `.Add(i); ` ` ` `return` `(` `set` `.Count % 2 == 0) ? 1 : 2; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String []args) ` ` ` `{ ` ` ` `int` `[]arr = { 1, 1, 2, 2, 2, 2 }; ` ` ` `Console.Write(` `"Player "` `+ playGame(arr) + ` `" wins"` `); ` ` ` `} ` `} ` ` ` `// This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

Player 1 Wins

**Time Complexity :** O(n)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Replacing an element makes array elements consecutive
- Array value by repeatedly replacing max 2 elements with their absolute difference
- Maximum sum of Array formed by replacing each element with sum of adjacent elements
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- Reconstruct the array by replacing arr[i] with (arr[i-1]+1) % M
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Maximum possible GCD after replacing at most one element in the given array
- Array sum after replacing all occurrences of X by Y for Q queries
- Most frequent element in Array after replacing given index by K for Q queries
- Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Game Theory (Normal - form game) | Set 1 (Introduction)
- Check if Array elements can be maximized upto M by adding all elements from another array
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Combinatorial Game Theory | Set 2 (Game of Nim)
- Find original array from encrypted array (An array of sums of other elements)
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Average of remaining elements after removing K largest and K smallest elements from array

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.