Find the winner of the Game of removing odd or replacing even array elements | 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
function findWinner ( arr , N ) { let odd = 0 ; let even = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { odd ++ ; } else { even ++ ; } } if ( even == 0 ) { if ( odd % 2 == 0 ) { document . write ( " " ) ; } else if ( odd % 2 == 1 ) { document . write ( " " ) ; } } else if ( even == 1 && odd % 2 == 1 ) { document . write ( " " ) ; } else { document . write ( - 1 ) ; } } let arr = [ 3 , 1 , 9 , 7 ] ; let N = arr . length ; findWinner ( arr , N ) ;