Maximize remainder of sum of a pair of array elements with different parity modulo K | Python3 program for the above approach ; Function to find the maximum remainder of sum of a pair of array elements modulo K ; Stores all even numbers ; Stores all odd numbers ; Segregate remainders of even and odd numbers in respective sets ; Stores the maximum remainder obtained ; Find the complement of remainder of each even number in odd set ; Find the complement of remainder x ; Print the answer ; Driver code ; Given array ; Size of the array ; Given value of K
from bisect import bisect_left NEW_LINE def maxRemainder ( A , N , K ) : NEW_LINE INDENT even = { } NEW_LINE odd = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT num = A [ i ] NEW_LINE if ( num % 2 == 0 ) : NEW_LINE INDENT even [ num % K ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT odd [ num % K ] = 1 NEW_LINE DEDENT DEDENT max_rem = 0 NEW_LINE for x in even : NEW_LINE INDENT y = K - 1 - x NEW_LINE od = list ( odd . keys ( ) ) NEW_LINE it = bisect_left ( od , y ) NEW_LINE if ( it != 0 ) : NEW_LINE INDENT max_rem = max ( max_rem , x + od [ it ] ) NEW_LINE DEDENT DEDENT print ( max_rem ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 4 , 11 , 6 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE K = 7 NEW_LINE maxRemainder ( arr , N , K ) NEW_LINE DEDENT