Check if sum of exactly K elements of the Array can be odd or not | Function returns true if it is possible to have odd sum ; Counting number of odd and even elements ; Driver Code
function isPossible ( arr , N , K ) { let oddCount = 0 , evenCount = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { evenCount ++ ; } else { oddCount ++ ; } } if ( evenCount == N || ( oddCount == N && K % 2 == 0 ) || ( K == N && oddCount % 2 == 0 ) ) { return false ; } else { return true ; } } let arr = [ 1 , 2 , 3 , 4 , 5 , 8 ] ; let K = 5 ; let N = arr . length ; if ( isPossible ( arr , N , K ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }