1386. Maximum XOR of Two Numbers in Array

HardBit ManipulationXOR TrieGreedyBit Manipulation

Given an array of non-negative integers, return the largest value of nums[i] XOR nums[j] over all pairs of positions. The intended solution builds the answer bit by bit from the most significant end using a binary trie or prefix set.

Input: A JSON object {"nums": [<non-negative integers>]}.

Output: Return the maximum pairwise XOR value.

Examples

Example 1
Input: {"nums":[3,10,5,25,2,8]}
Output: 28
Explanation: 5 XOR 25 = 28 is the largest pairwise XOR.
Example 2
Input: {"nums":[14,70,53,83,49,91,36,80,92,51,66,70]}
Output: 127
Explanation: The maximum pairwise XOR is 127.

Constraints

Asked by

GoogleBloombergAmazonMicrosoftMeta
Solve this problem in the editor →