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
< ? php $ INT_BITS = 32 ; function maxSubarrayXOR ( & $ set , $ n ) { global $ INT_BITS ; $ index = 0 ; for ( $ i = $ INT_BITS - 1 ; $ i >= 0 ; $ i -- ) { $ maxInd = $ index ; $ maxEle = 0 ; for ( $ j = $ index ; $ j < $ n ; $ j ++ ) { if ( ( $ set [ $ j ] & ( 1 << $ i ) ) != 0 && $ set [ $ j ] > $ maxEle ) { $ maxEle = $ set [ $ j ] ; $ maxInd = $ j ; } } if ( $ maxEle == 0 ) continue ; $ t = $ set [ $ index ] ; $ set [ $ index ] = $ set [ $ maxInd ] ; $ set [ $ maxInd ] = $ t ; $ maxInd = $ index ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ j != $ maxInd && ( $ set [ $ j ] & ( 1 << $ i ) ) != 0 ) $ set [ $ j ] = $ set [ $ j ] ^ $ set [ $ maxInd ] ; } $ index ++ ; } $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ res ^= $ set [ $ i ] ; return $ res ; } $ set = array ( 9 , 8 , 5 ) ; $ n = sizeof ( $ set ) ; echo " Max ▁ subset ▁ XOR ▁ is ▁ " ; echo maxSubarrayXOR ( $ set , $ n ) ; ? >