Two player game in which a player can remove all occurrences of a number | C ++ implementation for Two player game in which a player can remove all occurrences of a number ; Function that print whether player1 can wins or loses ; storing the number of occurrence of elements in unordered map ; variable to check if the occurrence of repeated elements is >= 4 and multiple of 2 or not ; count elements which occur more than once ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void game ( int v [ ] , int n ) { unordered_map < int , int > m ; 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 ; int check = 0 ; 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 ; } int main ( ) { int arr [ ] = { 3 , 2 , 2 , 3 , 3 , 5 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; game ( arr , size ) ; return 0 ; }