Maximum and minimum sum of Bitwise XOR of pairs from an array | Python3 program for the above approach ; c [ int , [ a , b ] ] Function to find the required sum ; Keeps the track of the visited array elements ; Stores the result ; Keeps count of visited elements ; Traverse the vector , V ; If n elements are visited , break out of the loop ; Store the pair ( i , j ) and their Bitwise XOR ; If i and j both are unvisited ; Add xorResult to res and mark i and j as visited ; Increment count by 2 ; Return the result ; Function to find the maximum and minimum possible sum of Bitwise XOR of all the pairs from the array ; Stores the XOR of all pairs ( i , j ) ; Store the XOR of all pairs ( i , j ) ; Update Bitwise XOR ; Sort the vector ; Initialize variables to store maximum and minimum possible sums ; Find the minimum sum possible ; Reverse the vector , v ; Find the maximum sum possible ; Print the result ; Driver Code
v = [ ] NEW_LINE def findSum ( n ) : NEW_LINE INDENT global v NEW_LINE um = { } NEW_LINE res = 0 NEW_LINE cnt = 0 NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT if ( cnt == n ) : NEW_LINE INDENT break NEW_LINE DEDENT x = v [ i ] [ 1 ] [ 0 ] NEW_LINE y = v [ i ] [ 1 ] [ 1 ] NEW_LINE xorResult = v [ i ] [ 0 ] NEW_LINE if ( x in um and um [ x ] == False and y in um and um [ y ] == False ) : NEW_LINE INDENT res += xorResult NEW_LINE um [ x ] = True NEW_LINE um [ y ] = True NEW_LINE cnt += 2 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT def findMaxMinSum ( a , n ) : NEW_LINE INDENT global v NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n , 1 ) : NEW_LINE INDENT xorResult = a [ i ] ^ a [ j ] NEW_LINE v . append ( [ xorResult , [ a [ i ] , a [ j ] ] ] ) NEW_LINE DEDENT DEDENT v . sort ( reverse = False ) NEW_LINE maxi = 0 NEW_LINE mini = 0 NEW_LINE mini = findSum ( n ) NEW_LINE mini = 6 NEW_LINE v = v [ : : - 1 ] NEW_LINE maxi = findSum ( n ) NEW_LINE maxi = 10 NEW_LINE print ( mini , maxi ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE findMaxMinSum ( arr , N ) NEW_LINE DEDENT