421. Maximum XOR of Two Numbers in an Array LeetCode Solution
In this guide, you will get 421. Maximum XOR of Two Numbers in an Array LeetCode Solution with the best time and space complexity. The solution to Maximum XOR of Two Numbers in an Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum XOR of Two Numbers in an Array solution in C++
- Maximum XOR of Two Numbers in an Array solution in Java
- Maximum XOR of Two Numbers in an Array solution in Python
- Additional Resources

Problem Statement of Maximum XOR of Two Numbers in an Array
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]
Output: 28
Explanation: The maximum result is 5 XOR 25 = 28.
Example 2:
Input: nums = [14,70,53,83,49,91,36,80,92,51,66,70]
Output: 127
Constraints:
1 <= nums.length <= 2 * 105
0 <= nums[i] <= 231 – 1
Complexity Analysis
- Time Complexity: O(n\log_2(\max(\texttt{nums})))
- Space Complexity: O(n)
421. Maximum XOR of Two Numbers in an Array LeetCode Solution in C++
class Solution {
public:
int findMaximumXOR(vector<int>& nums) {
const int maxNum = ranges::max(nums);
if (maxNum == 0)
return 0;
const int maxBit = static_cast<int>(log2(maxNum));
int ans = 0;
int prefixMask = 0; // Grows like: 10000 -> 11000 -> ... -> 11111.
// If ans is 11100 when i = 2, it means that before we reach the last two
// bits, 11100 is the maximum XOR we have, and we're going to explore if we
// can get another two 1s and put them into `ans`.
for (int i = maxBit; i >= 0; --i) {
prefixMask |= 1 << i;
unordered_set<int> prefixes;
// We only care about the left parts,
// If i = 2, nums = {1110, 1011, 0111}
// -> prefixes = {1100, 1000, 0100}
for (const int num : nums)
prefixes.insert(num & prefixMask);
// If i = 1 and before this iteration, the ans is 10100, it means that we
// want to grow the ans to 10100 | 1 << 1 = 10110 and we're looking for
// XOR of two prefixes = candidate.
const int candidate = ans | 1 << i;
for (const int prefix : prefixes)
if (prefixes.contains(prefix ^ candidate)) {
ans = candidate;
break;
}
}
return ans;
}
};
/* code provided by PROGIEZ */
421. Maximum XOR of Two Numbers in an Array LeetCode Solution in Java
class Solution {
public int findMaximumXOR(int[] nums) {
final int maxNum = Arrays.stream(nums).max().getAsInt();
if (maxNum == 0)
return 0;
final int maxBit = (int) (Math.log(maxNum) / Math.log(2));
int ans = 0;
int prefixMask = 0; // Grows like: 10000 -> 11000 -> ... -> 11111.
// If ans is 11100 when i = 2, it means that before we reach the last two
// bits, 11100 is the maximum XOR we have, and we're going to explore if we
// can get another two 1s and put them into `ans`.
for (int i = maxBit; i >= 0; --i) {
prefixMask |= 1 << i;
Set<Integer> prefixes = new HashSet<>();
// We only care about the left parts,
// If i = 2, nums = {1110, 1011, 0111}
// . prefixes = {1100, 1000, 0100}
for (final int num : nums)
prefixes.add(num & prefixMask);
// If i = 1 and before this iteration, the ans is 10100, it means that we
// want to grow the ans to 10100 | 1 << 1 = 10110 and we're looking for
// XOR of two prefixes = candidate.
final int candidate = ans | 1 << i;
for (final int prefix : prefixes)
if (prefixes.contains(prefix ^ candidate)) {
ans = candidate;
break;
}
}
return ans;
}
}
// code provided by PROGIEZ
421. Maximum XOR of Two Numbers in an Array LeetCode Solution in Python
class Solution:
def findMaximumXOR(self, nums: list[int]) -> int:
maxNum = max(nums)
if maxNum == 0:
return 0
maxBit = int(math.log2(maxNum))
ans = 0
prefixMask = 0 # `prefixMask` grows like: 10000 -> 11000 -> ... -> 11111.
# If ans is 11100 when i = 2, it means that before we reach the last two
# bits, 11100 is the maximum XOR we have, and we're going to explore if we
# can get another two 1s and put them into `ans`.
for i in range(maxBit, -1, -1):
prefixMask |= 1 << i
# We only care about the left parts,
# If i = 2, nums = [1110, 1011, 0111]
# -> prefixes = [1100, 1000, 0100]
prefixes = set([num & prefixMask for num in nums])
# If i = 1 and before this iteration, the ans is 10100, it means that we
# want to grow the ans to 10100 | 1 << 1 = 10110 and we're looking for
# XOR of two prefixes = candidate.
candidate = ans | 1 << i
for prefix in prefixes:
if prefix ^ candidate in prefixes:
ans = candidate
break
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.