Difference between sum of K maximum even and odd array elements | Function to find the absolute difference between sum of first K maximum even and odd numbers ; Stores index from where odd number starts ; Segregate even and odd number ; If current element is even ; Sort in decreasing order even part ; Sort in decreasing order odd part ; Calculate sum of k maximum even number ; Calculate sum of k maximum odd number ; Print the absolute difference ; Given array [ ] arr ; Size of array ; Function Call
def evenOddDiff ( a , n , k ) : NEW_LINE INDENT j = - 1 NEW_LINE even = [ ] NEW_LINE odd = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] % 2 == 0 ) : NEW_LINE INDENT even . append ( a [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT odd . append ( a [ i ] ) NEW_LINE DEDENT DEDENT j += 1 NEW_LINE even . sort ( ) NEW_LINE even . reverse ( ) NEW_LINE odd . sort ( ) NEW_LINE odd . reverse ( ) NEW_LINE evenSum , oddSum = 0 , 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT evenSum += even [ i ] NEW_LINE DEDENT for i in range ( k ) : NEW_LINE INDENT oddSum += odd [ i ] NEW_LINE DEDENT print ( abs ( evenSum - oddSum ) ) NEW_LINE DEDENT arr = [ 1 , 8 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE K = 2 NEW_LINE evenOddDiff ( arr , N , K ) NEW_LINE