Leetcode 421. 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
.
nums
:
nums
be?nums
take?These questions will help us understand the potential computational complexity and any edge cases we need to handle.
(i, j)
that produce the maximum XOR?1
only where bits differ, i.e., 1 XOR 0 = 1
and 0 XOR 1 = 1
, otherwise it outputs 0
.nums
into the trie.nums
, try to find the maximum XOR it can get with numbers already in the trie by traversing the trie in a way that tries to maximize the differing bits.n
is the number of elements and L
is the number of bits (typically 32 for integers). Finding the maximum XOR can also be done in O(n * L).Here’s the implementation in C++ using a Trie (prefix tree):
#include <iostream>
#include <vector>
using namespace std;
class TrieNode {
public:
TrieNode* children[2]; // 0 and 1
TrieNode() {
children[0] = children[1] = nullptr;
}
};
class Solution {
public:
TrieNode* root;
Solution() {
root = new TrieNode();
}
void insert(int num) {
TrieNode* node = root;
for (int i = 31; i >= 0; i--) {
int bit = (num >> i) & 1;
if (!node->children[bit]) {
node->children[bit] = new TrieNode();
}
node = node->children[bit];
}
}
int findMaximumXOR(const vector<int>& nums) {
// Insert all numbers into the Trie
for (int num : nums) {
insert(num);
}
int maxXOR = 0;
// Find the maximum XOR for each number with the Trie
for (int num : nums) {
TrieNode* node = root;
int currentXOR = 0;
for (int i = 31; i >= 0; i--) {
int bit = (num >> i) & 1;
// To maximize XOR, we look for the opposite bit (1 - bit)
if (node->children[1 - bit]) {
currentXOR = (currentXOR << 1) | 1;
node = node->children[1 - bit];
} else {
currentXOR = (currentXOR << 1);
node = node->children[bit];
}
}
maxXOR = max(maxXOR, currentXOR);
}
return maxXOR;
}
};
int main() {
Solution sol;
vector<int> nums = {3, 10, 5, 25, 2, 8};
cout << "Maximum XOR: " << sol.findMaximumXOR(nums) << endl; // Output should be 28
return 0;
}
n
is the number of elements and L
is the number of bits needed to represent the largest number (typically 32 for most 32-bit integers).This should solve the problem efficiently for typical input sizes.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?