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
function maximumXOR ( arr , n , K ) { K ++ ; let maxXor = Number . MIN_SAFE_INTEGER ; for ( let i = 0 ; i < ( 1 << n ) ; i ++ ) { if ( ( i ) . toString ( 2 ) . split ( ' ' ) . filter ( x => x == ' ' ) . length == K ) { let cur_xor = 0 ; for ( let j = 0 ; j < n ; j ++ ) { if ( i & ( 1 << j ) ) cur_xor = cur_xor ^ arr [ j ] ; } maxXor = Math . max ( maxXor , cur_xor ) ; } } return maxXor ; } let arr = [ 1 , 2 , 3 , 4 ] ; let N = arr . length ; let M = 2 ; document . write ( maximumXOR ( arr , N , M ) ) ;