Check if K distinct array elements form an odd sum | Function to return if odd sum is possible or not ; Stores distinct odd elements ; Stores distinct even elements ; Iterating through given array ; If element is even ; If element is odd ; If atleast K elements in the array are odd ; Check for all odd frequencies of odd elements whether sufficient even numbers are present or not ; Count of even numbers required ; If required even numbers are present in the array ; Driver code
function oddSum ( A , N , K ) { let Odd = new Set ( ) ; let Even = new Set ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) { Even . add ( A [ i ] ) ; } else { Odd . add ( A [ i ] ) ; } } if ( Odd . size >= K ) return true ; let flag = false ; for ( let i = 1 ; i < K ; i += 2 ) { let needed = K - i ; if ( needed <= Even . size ) { return true ; } } return flag ; } let K = 5 ; let A = [ 12 , 1 , 7 , 7 , 26 , 18 ] ; let N = 3 ; if ( oddSum ( A , N , K ) ) document . write ( " " ) ; else document . write ( " " ) ;