# Two player game in which a player can remove all occurrences of a number

Two players **player1** and **player2** are playing a game on a given number sequence **S** where **player1** starts first and both of them play optimally. The task is to find whether **player1** wins or loses. If he wins print “Yes” otherwise print “No”.

The rules of the game are as follows:

- The player’s alternate turns.
- In each turn, the current player must choose one or more elements of the current sequence S such that the values of all chosen elements are identical, and erase these elements from S.
- When a player cannot choose anything (the sequence S is already empty), this player loses the game.

**Examples:**

Input:S = {2, 1, 2, 2}

Output:Yes

Explanation:

The first player can choose the subset of elements with indices {0, 3} {0, 2} or {2, 3} (in the sequence) to ensure his victory in the future.

Input:S = {3, 2, 2, 3, 3, 5}

Output:No

Explanation:

The first player can not win in this sequence.

Ifplayer1chooses 2 and remove all the 2 from sequence thenplayer2will choose two 3 and remove only two 3 as both players play optimally.

Thenplayer1removes either 3 or 5 andplayer2will remove the final element. Soplayer1will always lose.

**Approach:** The approach to the above problem can be given as, if the total number of elements in sequence is even and the number of elements that occur more than once is also even then the first player can not win. If the number of elements that occur more than once is odd and the occurrence of repeated elements is greater than or equal to 4 and multiple of 2, then the first player can not win. Otherwise, **player1** can be the winner.

Below is the implementation of the above approach:

`// C++ implementation for Two player ` `// game in which a player can remove ` `// all occurrences of a number ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that print whether ` `// player1 can wins or loses ` `void` `game(` `int` `v[], ` `int` `n) ` `{ ` ` ` `unordered_map<` `int` `, ` `int` `> m; ` ` ` ` ` `// storing the number of occurrence ` ` ` `// of elements in unordered map ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `if` `(m.find(v[i]) == m.end()) ` ` ` `m[v[i]] = 1; ` ` ` ` ` `else` ` ` `m[v[i]]++; ` ` ` `} ` ` ` ` ` `int` `count = 0; ` ` ` ` ` `// variable to check if the ` `// occurrence of repeated ` `// elements is >= 4 and ` ` ` `// multiple of 2 or not ` ` ` `int` `check = 0; ` ` ` ` ` `// count elements which ` `// occur more than once ` ` ` `for` `(` `auto` `i: m) { ` ` ` `if` `(i.second > 1) { ` ` ` ` ` `if` `(i.second >= 4 ` `&& i.second % 2 == 0) ` ` ` `check++; ` ` ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `if` `(check % 2 != 0) ` ` ` `bool` `flag = ` `false` `; ` ` ` ` ` `if` `(check % 2 != 0) ` ` ` `cout << ` `"Yes"` `<< endl; ` ` ` ` ` `else` `if` `(n % 2 == 0 ` `&& count % 2 == 0) ` ` ` `cout << ` `"No"` `<< endl; ` ` ` ` ` `else` ` ` `cout << ` `"Yes"` `<< endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `arr[] = { 3, 2, 2, 3, 3, 5 }; ` ` ` ` ` `int` `size = ` `sizeof` `(arr) ` `/ ` `sizeof` `(arr[0]); ` ` ` ` ` `game(arr, size); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

No

**Time complexity:** The complexity of above approach is 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:

- Game of N stones where each player can remove 1, 3 or 4
- Find the player who will win the Coin game
- Find the minimum possible health of the winning player
- Find the player who rearranges the characters to get a palindrome string first
- Find probability that a player wins when probabilities of hitting the target are given
- Remove all occurrences of any element for maximum array sum
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)
- Game Theory (Normal - form game) | Set 1 (Introduction)
- Count number of occurrences (or frequency) in a sorted array
- Count subarrays with equal number of occurrences of two given elements
- Count occurrences of the average of array elements with a given number
- Find the number of occurrences of a character upto preceding position
- Number of ways for playing first move optimally in a NIM game
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Combinatorial Game Theory | Set 2 (Game of Nim)
- Remove minimum number of elements such that no common element exist in both 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.