Python coding

Largest XOR under a cap

You get a list nums of non-negative integers and a list of queries, each [x, cap]. For each query, return the largest value of x ^ v (bitwise XOR) over the numbers v of nums with v ≤ cap, or -1 if no number is that small. Answer the queries in the order given.

nums has at least one number, all values are below 2³⁰, and queries may be empty. With thousands of numbers and queries, scanning nums for every query is too slow: aim for O((n + q) · bits) after sorting.

Besides the tests, one large input (7,500 numbers, 7,500 queries) with a ~1000 ms limit (scaled to your device's speed): brute force won't pass.

  • max_xor_capped([6,13,4,22,1,9], [[5,10],[5,3],[5,0],[12,30]]) → [12,4,-1,26]
  • max_xor_capped([7,1,4], [[2,0],[2,4],[6,100]]) → [-1,6,7]
  • max_xor_capped([6], [[1,6],[1,5]]) → [7,-1]
Ctrl/⌘ + Enter runs · Tab indents
Hint 1

Without the cap this is the binary-trie greedy: insert the numbers highest bit first, then walk toward the opposite bit of x whenever that child exists.

Hint 2

The cap decides which numbers may be in the trie. Sort nums, answer the queries offline by increasing cap, and before each one insert every number up to its cap.

Hint 3

The trie only grows, so each number is inserted once. If nothing has been inserted yet the answer is -1; store each answer at the query's original index.

Solution and why it works
pythonO(n log n + q log q + (n + q) · B), B = bits · O(n · B)
def max_xor_capped(nums: list[int], queries: list[list[int]]) -> list[int]:
    nums = sorted(set(nums))                                   # a repeated number adds nothing
    bits = max([*nums, *(x for x, _ in queries)]).bit_length()
    root, i, answers = [None, None], 0, [-1] * len(queries)    # node = [child for 0, child for 1]
    for q in sorted(range(len(queries)), key=lambda q: queries[q][1]):    # smallest cap first
        x, cap = queries[q]
        while i < len(nums) and nums[i] <= cap:                # insert every number this cap allows
            node = root
            for b in range(bits - 1, -1, -1):
                bit = (nums[i] >> b) & 1
                if node[bit] is None:
                    node[bit] = [None, None]
                node = node[bit]
            i += 1
        if i == 0:
            continue                                           # no number is small enough: stays -1
        node, best = root, 0
        for b in range(bits - 1, -1, -1):                      # greedy: take the opposite bit if stored
            want = 1 - ((x >> b) & 1)
            if node[want] is not None:
                best |= 1 << b
                node = node[want]
            else:
                node = node[1 - want]
        answers[q] = best
    return answers

Sorting the queries by cap turns v ≤ cap into a prefix of the sorted nums, so the trie only grows: each number is inserted once, and each query is one greedy walk that takes the opposite bit of x whenever it is stored, since one higher bit outweighs all the lower ones. Traps: returning the answers in sorted order instead of the original one, and counting bits from nums alone, when x may have higher bits that the answer keeps.