Maximum Bitwise XOR of node values of an Acyclic Graph made up of N given vertices using M edges | Function to find the maximum Bitwise XOR of any subset of the array of size K ; Number of node must K + 1 for K edges ; Stores the maximum Bitwise XOR ; Generate all subsets of the array ; __builtin_popcount ( ) returns the number of sets bits in an integer ; Initialize current xor as 0 ; If jth bit is set in i then include jth element in the current xor ; Update the maximum Bitwise XOR obtained so far ; Return the maximum XOR ; Driver Code
def maximumXOR ( arr , n , K ) : NEW_LINE INDENT K += 1 NEW_LINE maxXor = - 10 ** 9 NEW_LINE for i in range ( 1 << n ) : NEW_LINE INDENT if ( bin ( i ) . count ( '1' ) == K ) : NEW_LINE INDENT cur_xor = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( i & ( 1 << j ) ) : NEW_LINE INDENT cur_xor = cur_xor ^ arr [ j ] NEW_LINE DEDENT DEDENT maxXor = max ( maxXor , cur_xor ) NEW_LINE DEDENT DEDENT return maxXor NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE M = 2 NEW_LINE print ( maximumXOR ( arr , N , M ) ) NEW_LINE DEDENT