# Problem 421: Maximum XOR of Two Numbers in an Array

> <https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/?tab=Description>

## 思路

* 看了答案还是有些不太明白，下面这个讲解比较清楚

> <https://discuss.leetcode.com/topic/63213/java-o-n-solution-using-bit-manipulation-and-hashmap>

```java
public class Solution {
    public int findMaximumXOR(int[] nums) {
        int mask = 0, max = 0;
        for (int i = 31; i >= 0; i--) {
            mask |= (1 << i);
            HashSet<Integer> set = new HashSet<Integer>();
            for (int num : nums) {
                set.add(num & mask);
            }

            int tmp = (max | (1 << i));
            for (int prefix : set) {
                if (set.contains(tmp ^ prefix)) {
                    max = tmp;
                }
            }
        }

        return max;
    }
}
```


---

# Agent Instructions: 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://liuyang89116.gitbook.io/my-leetcode-book/post_chapter_2_math/bit-manipulation/problem-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.
