Bit manipulation · Hard
A test generator wants the pair of recorded masks with the greatest numeric XOR. It may contain one hundred thousand entries, so comparing all pairs is too expensive. This is a specialist bit-prefix exercise rather than a requirement for every engineering interview.
Return the largest unsigned XOR attainable using two different input indexes.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ nums: unsigned32[] }
Fields
- nums contains unsigned 32-bit integers from 0 through 4294967295. Values with bit 31 set are positive JSON integers.
Output
Return value
- Return the maximum unsigned XOR value, not the original pair or its indexes.
- Return null with fewer than two entries. Equal values at different indexes may form a pair whose XOR is zero.
Constraints
- 0 <= nums.length <= 100000.
Examples
Example 1
{
"nums": [
3,
10,
5,
25,
2,
8
]
}Returns
28Example 2
{
"nums": [
0,
4294967295
]
}Returns
4294967295Required complexity
O(32n) expected time and O(n) auxiliary space using prefix sets