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
def checkEvenSum ( arr , N , size ) : NEW_LINE INDENT even_freq , odd_freq = 0 , 0 NEW_LINE for i in range ( size ) : NEW_LINE INDENT if ( arr [ i ] & 1 ) : NEW_LINE INDENT odd_freq += 1 NEW_LINE DEDENT else : NEW_LINE INDENT even_freq += 1 NEW_LINE DEDENT DEDENT if ( even_freq >= N ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT if ( odd_freq & 1 ) : NEW_LINE INDENT taken = odd_freq - 1 NEW_LINE req = N - taken NEW_LINE if ( even_freq < req ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT taken = odd_freq NEW_LINE req = N - taken NEW_LINE if ( even_freq < req ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 9 , 2 , 3 , 4 , 18 , 7 , 7 , 6 ] NEW_LINE size = len ( arr ) NEW_LINE N = 5 NEW_LINE if ( checkEvenSum ( arr , N , size ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT