Game of replacing array elements | CPP program for Game of Replacement ; Function return which player win the game ; Create hash that will stores all distinct element ; Traverse an array element ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int playGame ( int arr [ ] , int n ) { unordered_set < int > hash ; for ( int i = 0 ; i < n ; i ++ ) hash . insert ( arr [ i ] ) ; return ( hash . size ( ) % 2 == 0 ? 1 : 2 ) ; } 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 ; }