> For the complete documentation index, see [llms.txt](https://protegejj.gitbook.io/algorithm-practice/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://protegejj.gitbook.io/algorithm-practice/leetcode/trie/421-maximum-xor-of-two-numbers-in-an-array.md).

# 421 Maximum XOR of Two Numbers in an Array

## 421. [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/description/)

## 1. Question

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

Find the maximum result of aiXOR 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.
```

## 2. Implementation

**(1) Brute Force**

```java
class Solution {
    public int findMaximumXOR(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }

        int max = 0;

        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                max = Math.max(max, nums[i] ^ nums[j]);
            }
        }
        return max;
    }
}
```

**(2) Bit Manipulation + Trie**

```java
class Solution {
    class TrieNode {
        TrieNode[] childNode;

        public TrieNode() {
            childNode = new TrieNode[2];
        }
    }

    class Trie {
        TrieNode root;

        public Trie() {
            root = new TrieNode();
        }

        public void insert(int num) {
            TrieNode curNode = root;
            for (int i = 31; i >= 0; i--) {
                int bit = (num >>> i) & 1;

                if (curNode.childNode[bit] == null) {
                    curNode.childNode[bit] = new TrieNode();
                }
                curNode = curNode.childNode[bit];
            }
        }
    }

    public int findMaximumXOR(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }

        Trie trie = new Trie();

        for (int num : nums) {
            trie.insert(num);
        }

        int max = 0;

        for (int num : nums) {
            int curSum = 0;
            TrieNode curNode = trie.root;

            for (int i = 31; i >= 0; i--) {
                int bit = (num >>> i) & 1;

                if (curNode.childNode[bit ^ 1] != null) {
                    curSum |= (1 << i);
                    curNode = curNode.childNode[bit ^ 1];
                }
                else {
                    curNode = curNode.childNode[bit];
                }
            }

            max = Math.max(max, curSum);
        }
        return max;
    }
}
```

## 3. Time & Space Complexity

**Brute Force:** 时间复杂度O(n^2), 空间复杂度O(1)

**Bit Manipulation + Trie:** 时间复杂度O(n), 空间复杂度O(n)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://protegejj.gitbook.io/algorithm-practice/leetcode/trie/421-maximum-xor-of-two-numbers-in-an-array.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
