Maximum sum of Bitwise XOR of all elements of two equal length subsets | Function that finds the maximum Bitwise XOR sum of the two subset ; Check if the current state is already computed ; Initialize answer to minimum value ; Iterate through all possible pairs ; Check whether ith bit and jth bit of mask is not set then pick the pair ; For all possible pairs find maximum value pick current a [ i ] , a [ j ] and set i , j th bits in mask ; Store the maximum value and return the answer ; Driver Code ; Given array arr [ ] ; Declare Initialize the dp states ; Function call
def xorSum ( a , n , mask , dp ) : NEW_LINE INDENT if ( dp [ mask ] != - 1 ) : NEW_LINE INDENT return dp [ mask ] NEW_LINE DEDENT max_value = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( i != j and ( mask & ( 1 << i ) ) == 0 and ( mask & ( 1 << j ) ) == 0 ) : NEW_LINE INDENT max_value = max ( max_value , ( a [ i ] ^ a [ j ] ) + xorSum ( a , n , ( mask | ( 1 << i ) | ( 1 << j ) ) , dp ) ) NEW_LINE DEDENT DEDENT DEDENT dp [ mask ] = max_value NEW_LINE return dp [ mask ] NEW_LINE DEDENT n = 4 NEW_LINE arr = [ 1 , 2 , 3 , 4 ] NEW_LINE dp = [ - 1 ] * ( ( 1 << n ) + 5 ) NEW_LINE print ( xorSum ( arr , n , 0 , dp ) ) NEW_LINE