> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Maximum XOR of Two Numbers in an Array

> Tested Python solution for LeetCode 421 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 421, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Bit Manipulation](/catalog/topics/bit-manipulation), [Trie](/catalog/topics/trie). [View on LeetCode](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 421   # by problem number
lcpy gen -s maximum_xor_of_two_numbers_in_an_array   # by problem name
```

## Problem

Given an integer array `nums`, return the maximum result of `nums[i] XOR nums[j]`, where `0 <= i <= j < n`.

### Examples

```
Input: nums = [3,10,5,25,2,8]
Output: 28
Explanation: The maximum result is 5 XOR 25 = 28.
```

```
Input: nums = [14,70,53,83,49,91,36,80,92,51,66,70]
Output: 127
```

### Constraints

* 1 \<= nums.length \<= 2 \* 10^5
* 0 \<= nums\[i] \<= 2^31 - 1

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_xor_of_two_numbers_in_an_array/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_xor_of_two_numbers_in_an_array/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * 31)
    # Space: O(n * 31)
    def find_maximum_xor(self, nums: list[int]) -> int:
        root: dict[int, dict] = {}

        def insert(word: int) -> None:
            node = root
            for bit in range(30, -1, -1):
                node = node.setdefault((word >> bit) & 1, {})

        result = 0
        insert(nums[0])
        for num in nums[1:]:
            node = root
            best = 0
            for bit in range(30, -1, -1):
                key = (num >> bit) & 1
                if (1 - key) in node:
                    best |= 1 << bit
                    node = node[1 - key]
                else:
                    node = node[key]
            result = max(result, best)
            insert(num)
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n \* 31) | O(n \* 31) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.