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 Program
def oddSum ( A , N , K ) : NEW_LINE INDENT Odd = set ( [ ] ) NEW_LINE Even = set ( [ ] ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( A [ i ] % 2 == 0 ) : NEW_LINE INDENT Even . add ( A [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT Odd . add ( A [ i ] ) NEW_LINE DEDENT DEDENT if ( len ( Odd ) >= K ) : NEW_LINE INDENT return True NEW_LINE DEDENT flag = False NEW_LINE for i in range ( 1 , K , 2 ) : NEW_LINE INDENT needed = K - i NEW_LINE if ( needed <= len ( Even ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return flag NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT K = 5 NEW_LINE A = [ 12 , 1 , 7 , 7 , 26 , 18 ] NEW_LINE N = 3 NEW_LINE if ( oddSum ( A , N , K ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT