Bonus Day 100MediumLeetCode

Maximum XOR of Two Numbers in an Array

Find the largest value of nums[i] XOR nums[j] over all pairs in the array.

Pattern
Binary trie, greedy opposite bit
Time box
25 min
Time
O(31 · n) = O(n)
Space
O(31 · n)

Try it first — give it 25 minutes

0/4 revealed

Stuck? Take one hint at a time. The struggle is where the pattern sticks.

    The problem in plain English

    You get an array nums of non-negative integers, each less than 2³¹. Return the maximum of nums[i] XOR nums[j] over all pairs i, j. (i = j is allowed and gives 0, so the answer is at least 0.) The array can have up to 2 × 10⁵ numbers.

    Example: nums = [3, 10, 5, 25, 2, 8] → 28, from 5 XOR 25:

     5 = 00101
    25 = 11001
    XOR  11100 = 28

    Start with brute force

    Try every pair and keep the largest XOR. That's n²/2 pairs: for n = 2 × 10⁵, about 2 × 10¹⁰ XORs. Far too slow.

    Building the intuition

    Fix one number x. Which partner y makes x ^ y biggest?

    XOR gives a 1 wherever the bits differ. And in binary, one high bit beats everything below it: 10000 (16) is bigger than 01111 (15). So the most important thing is to make the highest bit of x ^ y a 1. Only once that's settled does the next bit matter, and so on.

    That's a greedy plan. Look at x's bits from bit 30 down to bit 0, and at each step prefer a partner whose bit is the opposite of x's:

    • if some number in the array matches all our choices so far and has the opposite bit here, take it: this bit of the answer is 1;
    • otherwise, we're forced to take the same bit: this bit of the answer is 0.

    The question at each step is "does any number start with this pattern of bits?" That's exactly what a trie answers: Implement Trie's startsWith, but with an alphabet of two letters, 0 and 1. Each number becomes a 31-letter word read from its highest bit.

    Why must the bits go from high to low? Because that's the order of importance. Greedy is safe only when an early choice can never be outweighed by later ones, and a 1 at bit k is worth more than bits k−1 to 0 combined.

    The optimal approach

    1. Create an empty binary trie. Each node has two children, children[0] and children[1].
    2. For each x in nums:
      • Insert x: for bit from 30 down to 0, let b = (x >> bit) & 1, create the child if needed, and move into it.
      • Query x: start at the root with cur = 0. For each bit from 30 down to 0, let b be x's bit. If children[1 − b] exists, set that bit in cur (cur |= 1 << bit) and move there. Otherwise move to children[b].
      • best = max(best, cur).
    3. Return best.

    The subtle detail: we insert x before querying it. That way the trie is never empty, and the fallback children[b] always exists (x's own path is there). Every pair is still covered: when the later number of a pair is queried, the earlier one is already in the trie.

    Why 31 bits? The values are below 2³¹, so bit 30 is the highest bit that can be set.

    The code

    Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.

    class Solution:
        def findMaximumXOR(self, nums: List[int]) -> int:
            # Binary trie over the 31 bits of each number, highest bit first.
            # Each node is a dict {0: child, 1: child}; in Python a dict per node
            # is much faster than a class with a two-slot list.
            root = {}
            best = 0
            for x in nums:
                # Insert x first, so the trie is never empty when we query it
                node = root
                for bit in range(30, -1, -1):
                    b = (x >> bit) & 1
                    if b not in node:
                        node[b] = {}
                    node = node[b]
    
                # Walk down choosing the OPPOSITE bit whenever we can: a 1 at a high
                # bit beats any combination of lower bits, so greedy is safe
                node = root
                cur = 0
                for bit in range(30, -1, -1):
                    b = (x >> bit) & 1
                    if 1 - b in node:
                        cur |= 1 << bit
                        node = node[1 - b]
                    else:
                        node = node[b]   # always exists: x itself took this path
                best = max(best, cur)
            return best

    Dry run

    To keep it small, use 5 bits (bits 4 to 0) and nums = [3, 10, 5, 25], all already inserted. Query x = 5 (00101):

    bitx's bitwantexists?go tocur
    401yes (25 = 11001)110000
    301yes (25)111000
    210yes (25)011100
    101no (only 25 is left, and its bit 1 is 0)011100
    010no (25's bit 0 is 1)111100

    cur = 28, matching 5 XOR 25. The query walked to 25 without ever comparing 5 with 3 or 10 directly.

    Complexity

    • Time: O(31 · n) = O(n). Each number is inserted and queried once, 31 steps each.
    • Space: O(31 · n). In the worst case each number adds up to 31 new nodes. Shared high bits make it smaller in practice.

    Edge cases and traps

    • One element. The only pair is x ^ x = 0. Inserting before querying returns 0 correctly.
    • All equal, like [7, 7, 7]. Every XOR is 0. Works.
    • Going from the low bit up. The greedy only works high to low. Low to high can sacrifice a high bit for a low one.
    • Querying before any insert. If you query first, the trie is empty and children[b] can be null. Insert first, or handle the empty case.
    • Python speed. 2 × 10⁵ numbers × 31 bits × 2 walks is about 12 million steps. In Python, a plain dict per node, as below, is several times faster than a class with a two-slot list. The alternative in the Follow-ups is faster still.

    Say it in the interview

    "Brute force over pairs is O(n squared). For a fixed x, the best partner differs from x in the highest possible bit, because a high bit outweighs all the lower ones. So I put every number in a binary trie, reading bits from 30 down to 0. To query x, I walk the trie and take the opposite bit whenever that child exists, which sets that bit of the XOR. I insert each number before querying it, so the trie is never empty. That's 31 steps per number, so O(n) time and O(31n) space."

    Follow-ups interviewers ask

    • "Can you do it without a trie?" Yes, with prefix sets. Build the answer bit by bit from the top. At each bit, guess that it's 1, put every number's top bits into a hash set, and check whether any prefix p has p ^ guess in the set (the Two Sum idea, with XOR instead of minus). Also O(31 · n), and often faster in Python.
    • "Answer many queries: max XOR of x with any number in the array?" Build the trie once and run the query walk for each x: O(31) per query.
    • "Max XOR of a subarray?" XOR of a subarray is prefix[j] ^ prefix[i], so insert prefix XORs into the trie and query each one: the same problem.

    Practise the same idea

    This is a bonus day of the free DSA plan 🐸

    90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.

    Get the full roadmap — free