Find the maximum sum pair in an Array with even parity | Python3 implementation to find a pair with even parity and maximum sum ; Function that returns true if count of set bits in given number is even ; Parity will store the count of set bits ; Function to print the elements of the array ; Function to remove all the even parity elements from the given array ; Traverse the array ; If the current element has even parity ; Driver Code ; Function call
import sys NEW_LINE sz = 1e3 NEW_LINE def isEvenParity ( x ) : NEW_LINE INDENT parity = 0 NEW_LINE while x != 0 : NEW_LINE INDENT if ( x & 1 ) : NEW_LINE INDENT parity = parity + 1 ; NEW_LINE DEDENT x = x >> 1 NEW_LINE DEDENT if ( parity % 2 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def printArray ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT def findPairEvenParity ( arr , n ) : NEW_LINE INDENT firstMaximum = - 1 NEW_LINE secondMaximum = - 1 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if isEvenParity ( arr [ i ] ) == True : NEW_LINE INDENT if ( arr [ i ] >= firstMaximum ) : NEW_LINE INDENT secondMaximum = firstMaximum NEW_LINE firstMaximum = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] >= secondMaximum ) : NEW_LINE INDENT secondMaximum = arr [ i ] NEW_LINE DEDENT DEDENT DEDENT print ( firstMaximum , secondMaximum ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 18 , 15 , 8 , 9 , 14 ] NEW_LINE n = len ( arr ) NEW_LINE findPairEvenParity ( arr , n ) NEW_LINE DEDENT