Maximum XOR of Two Numbers in an Array

Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231.

Find the maximum result of ai XOR aj, where 0 ≤ i, j < n.

Could you do this in O(n) runtime?

Example:

Input: [3, 10, 5, 25, 2, 8]

Output: 28

Explanation: The maximum result is 5 ^ 25 = 28.

分析

a^b=c => a^c =b. 所以用ans^a = b => a^b = ans (a,b都是prevs)

每次这里只在乎每个Num的左半部分bit, ans >>=1 然后 ans^1( 就是ans+1)让ans下一位变成1,如果能找到ans^a也在prevs里,这该数b^a = ans

class Solution:
    def findMaximumXOR(self, nums: List[int]) -> int:
        ans = 0
        for i in range(32)[::-1]:
            prevs = [n>>i for n in nums]
            ans <<= 1
            ans += any(ans^1^p in prevs for p in prevs)
        return ans

Trie实现,只有2个子树0,1。每次尽量往相反方向走,0^1才能max

可行则 temp+=1<<j. 或者直接最后叶子的值^当前Num

Last updated

Was this helpful?