Medium
LABMaximum XOR of Two Numbers in an Array
Return the maximum xor obtainable from any pair of numbers.
EXAMPLES
Example 1
Input
{
"nums": [
3,
10,
5,
25,
2,
8
]
}
Output
28FUNCTION SHAPE
nums: intArray→intSOLUTION NOTE
For each number, we want to find another number that differs in as many significant bit positions as possible, maximizing XOR.
Reveal reference solution +
pythonREFERENCE
def findMaximumXOR(self, nums: List[int]) -> int:
trie = XORTrie()
for num in nums:
trie.insert(num)
max_xor = 0
for num in nums:
max_xor = max(max_xor, trie.findMaxXOR(num))
return max_xorTime
O(n)Space
O(n)