Check if N numbers with Even Sum can be selected from a given Array | Function to check if an odd sum can be made using N integers from the array ; Initialize odd and even counts ; Iterate over the array to count the no . of even and odd integers ; If element is odd ; If element is even ; Check if even_freq is more than N ; If odd_freq is odd ; Consider even count of odd ; Calculate even required ; If even count is less than required count ; Calculate even required ; If even count is less than required count ; Driver Code
function checkEvenSum ( arr , N , size ) { var even_freq = 0 , odd_freq = 0 ; for ( var i = 0 ; i < size ; i ++ ) { if ( arr [ i ] & 1 ) odd_freq ++ ; else even_freq ++ ; } if ( even_freq >= N ) return true ; else { if ( odd_freq & 1 ) { var taken = odd_freq - 1 ; var req = N - taken ; if ( even_freq < req ) { return false ; } else return true ; } else { var taken = odd_freq ; var req = N - taken ; if ( even_freq < req ) { return false ; } else return true ; } } return false ; } var arr = [ 9 , 2 , 3 , 4 , 18 , 7 , 7 , 6 ] ; var size = arr . length ; var N = 5 ; if ( checkEvenSum ( arr , N , size ) ) document . write ( " " ) ; else document . write ( " " ) ;