Maximum XOR With an Element From Array — LeetCode 1707 Python Solution

HardBit ManipulationTrieArray
Problem
#1707
Pattern
Trie
Reading time
7 min

The problem

You are given an array nums consisting of non-negative integers. You are also given a queries array, where queries[i] = [xi, mi].

Example

Input
nums = [0,1,2,3,4], queries = [[3,1],[1,3],[5,6]]
Output
[3,3,7]
Explanation
1) 0 and 1 are the only two integers not greater than 1. 0 XOR 3 = 3 and 1 XOR 3 = 2. The larger of the two is 3.

Python solution

Python
class Trie:
    __slots__ = ["children"]

    def __init__(self):
        self.children = [None] * 2

    def insert(self, x: int):
        node = self
        for i in range(30, -1, -1):
            v = x >> i & 1
            if node.children[v] is None:
                node.children[v] = Trie()
            node = node.children[v]

    def search(self, x: int) -> int:
        node = self
        ans = 0
        for i in range(30, -1, -1):
            v = x >> i & 1
            if node.children[v ^ 1]:
                ans |= 1 << i
                node = node.children[v ^ 1]
            elif node.children[v]:
                node = node.children[v]
            else:
                return -1
        return ans


class Solution:
    def maximizeXor(self, nums: List[int], queries: List[List[int]]) -> List[int]:
        trie = Trie()
        nums.sort()
        j, n = 0, len(queries)
        ans = [-1] * n
        for i, (x, m) in sorted(zip(range(n), queries), key=lambda x: x[1][1]):
            while j < len(nums) and nums[j] <= m:
                trie.insert(nums[j])
                j += 1
            ans[i] = trie.search(x)
        return ans

Complexity

MeasureComplexity
TimeO(m \times \log m + n \times (\log n + \log M))
SpaceO(n \times \log M) auxiliary

Pattern: Trie

Store a set of words by their shared prefixes so lookups cost the length of the word. LeetCode 1707. Maximum XOR With an Element From Array is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Trie.

The trie guide has the Python template for the pattern and the 49 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1707. Maximum XOR With an Element From Array?
LeetCode 1707. Maximum XOR With an Element From Array is rated Hard on LeetCode.
What is the time complexity of LeetCode 1707. Maximum XOR With an Element From Array?
The Python solution on this page runs in O(m \times \log m + n \times (\log n + \log M)).
What is the space complexity of LeetCode 1707. Maximum XOR With an Element From Array?
The Python solution on this page uses O(n \times \log M) auxiliary space.
What topics does LeetCode 1707. Maximum XOR With an Element From Array cover?
LeetCode 1707. Maximum XOR With an Element From Array is tagged Bit Manipulation, Trie and Array on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview