class Solution {
public int findMaximumXOR(int[] nums) {
int max = 0, mask = 0;
// search from left to right, find out for each bit is there
// two numbers that has different value
for (int i = 31; i >= 0; i--) {
// the mask woule be 1000..., 11000..., 111000...
mask |= (1 << i);
//System.out.println(Integer.toBinaryString(mask));
Set<Integer> set = new HashSet<>();
for (int num : nums) {
// store prefix of all number with right i bits discarded
set.add(num & mask);
//System.out.println(num + "," + Integer.toBinaryString(num) + ": " + Integer.toBinaryString(mask) + "," + Integer.toBinaryString(mask & num));
}
// now find out if there are two prefix with different i-th bit
// if there is, the new max should be current max with one 1 bit at i-th position, which is candidate
// and the two prefix, say A and B, satisfies:
// A ^ B = candidate
// so we also have A ^ candidate = B or B ^ candidate = A
// thus we can use this method to find out if such A and B exists in the set
int candidate = max | (1 << i);
for (int prefix : set) {
if (set.contains(candidate ^ prefix)) {
max = candidate;
break;
}
}
}
return max;
}
}