LC: https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/
public int findMaximumXOR(int[] nums) {
int max = 0, mask = 0;
for (int i = 31; i >= 0; i--){
mask = mask | (1 << i);
Set<Integer> set = new HashSet<>();
for(int num : nums){
int left = num & mask
set.add(left);
}
int greed = max | (1 << i);
for (int prefix : set){
if (set.contains(greed ^ prefix)) {
max = greed;
break;
}
}
}
return max;
}
Could someone explain what is going on when we apply the AND operator on nums[i] with what seems like a progressively smaller mask(Beginning with 2^31, 2^30...)
int left = num & mask
I know from the comments it's supposed to keep the left bits while ignoring the right bits but I'm still not sure what's happening behind this code.