# Leetcode 421 - Maximum XOR of Two Numbers in an Array

`Note:`

- Background knowledge:
`a ^ b = c`

,`c ^ a = b`

. - How to be
`greedy`

? Start from the most significant bit, set it to 1 as the`mask`

. Do`mask & num`

and add them all to a`map`

. - How to get the mask?
- Find
`max`

then flip all 0s to 1s, and calculate the length of it.

- Find
- If
`map.has(n & mask ^ mask)`

, it means there is another num that can do`a ^ b = mask`

. - If not, it means the digit should be
`0`

in mask. - Continue it until mask’s length === max.length.
- Visulize the process: As the process going, less and less nums would be left on both sides.

`Question:`

Given an integer array `nums`

, return the maximum result of `nums[i] XOR nums[j]`

, where `0 <= i <= j < n`

.

`Example:`

1 | Input: nums = [3,10,5,25,2,8] |

`Code:`

1 | /** |