Find the maximum subset XOR of a given set | Number of bits to represent int ; Function to return maximum XOR subset in set [ ] ; Initialize index of chosen elements ; Traverse through all bits of integer starting from the most significant bit ( MSB ) ; Initialize index of maximum element and the maximum element ; If i 'th bit of set[j]  is set and set[j] is  greater than max so far. ; If there was no element with i 'th  bit set, move to  smaller i ; Put maximum element with i ' th ▁ bit ▁ set ▁ ▁ at ▁ index ▁ ' index ' ; Update maxInd and increment index ; Do XOR of set [ maxIndex ] with all numbers having i 'th bit as set. ; XOR set [ maxInd ] those numbers which have the i 'th bit set ; Increment index of chosen elements ; Final result is XOR of all elements ; Driver code
let INT_BITS = 32 ; function maxSubarrayXOR ( set , n ) { let index = 0 ; for ( let i = INT_BITS - 1 ; i >= 0 ; i -- ) { let maxInd = index ; let maxEle = Number . MIN_VALUE ; for ( let j = index ; j < n ; j ++ ) { if ( ( set [ j ] & ( 1 << i ) ) != 0 && set [ j ] > maxEle ) { maxEle = set [ j ] ; maxInd = j ; } } if ( maxEle == Number . MIN_VALUE ) continue ; let temp = set [ index ] ; set [ index ] = set [ maxInd ] ; set [ maxInd ] = temp ; maxInd = index ; for ( let j = 0 ; j < n ; j ++ ) { if ( j != maxInd && ( set [ j ] & ( 1 << i ) ) != 0 ) set [ j ] = set [ j ] ^ set [ maxInd ] ; } index ++ ; } let res = 0 ; for ( let i = 0 ; i < n ; i ++ ) res ^= set [ i ] ; return res ; } let set = [ 9 , 8 , 5 ] ; let n = set . length ; document . write ( " " ) ; document . write ( maxSubarrayXOR ( set , n ) ) ;