Find the winner of the Game of removing odd or replacing even array elements | C ++ program for the above approach ; Function to evaluate the winner of the game ; Stores count of odd array elements ; Stores count of even array elements ; Traverse the array ; If array element is odd ; Otherwise ; If count of even is zero ; If count of odd is even ; If count of odd is odd ; If count of odd is odd and count of even is one ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findWinner ( int arr [ ] , int N ) { int odd = 0 ; int even = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { odd ++ ; } else { even ++ ; } } if ( even == 0 ) { if ( odd % 2 == 0 ) { cout << " Player ▁ 2" << endl ; } else if ( odd % 2 == 1 ) { cout << " Player ▁ 1" << endl ; } } else if ( even == 1 && odd % 2 == 1 ) { cout << " Player ▁ 1" << endl ; } else { cout << -1 << endl ; } } int main ( ) { int arr [ ] = { 3 , 1 , 9 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findWinner ( arr , N ) ; return 0 ; }