19. Maximum XOR of two numbers in an array
The problem can be found at the following link: Question Link
Problem Description
Given an array arr[] of non-negative integers, your task is to find the maximum XOR value that can be obtained by XORing any two distinct elements of the array.
Examples
Example 1:
Input:
arr[] = [25, 10, 2, 8, 5, 3]
Output:
28
Explanation:
The pair (25 ^ 5) gives the maximum XOR value which is 28.
Example 2:
Input:
arr[] = [1, 2, 3, 4, 5, 6, 7]
Output:
7
Explanation:
The pair (1 ^ 6) gives the maximum XOR value which is 7.
Constraints:
$(2 \leq \text{arr.size()} \leq 5 \times 10^4)$
$(1 \leq \text{arr[i]} \leq 10^6)$
My Approach
Greedy Bitmasking with HashSet
Iterate from MSB to LSB (31 to 0).
At each bit position:
Mask numbers to keep the current prefix.
Try setting the current bit in the result.
Use a set to check if there's a pair that can form this potential result.
This way, we greedily try to set each bit from left to right, ensuring it's possible.
Algorithm Steps:
Initialize
max_xor = 0andmask = 0.Loop over 31 bits from high to low.
For each bit:
Add the bit to the
mask.Store all prefixes using the current mask.
Try setting the bit in
max_xor, and see if two prefixes exist that form it.
If yes, update
max_xor.
Time and Auxiliary Space Complexity
Expected Time Complexity: O(32 × N), where N is the size of the array. We iterate through 32 bits and for each bit, we traverse the entire array once.
Expected Auxiliary Space Complexity: O(N), used to store prefixes in a hash set at each bit position.
Code (C++)
⚡ Alternative Approaches
📊 2️⃣ Trie-based Optimal
Algorithm Steps:
Build a Trie to store binary representations of numbers (MSB to LSB).
Insert all numbers into the Trie bit by bit.
Query each number against the Trie to find the maximum possible XOR by choosing opposite bits greedily.
📝 Complexity Analysis:
Time Complexity:
O(32n)Space Complexity:
O(32n)(Trie storage)
✅ Why This Approach?
Optimal for large datasets. Uses bitwise Trie to efficiently compute maximum XOR in linear time.
📊 3️⃣ Brute-force (Check All Pairs)
Algorithm Steps:
Check all possible pairs of elements.
Compute XOR for each pair and track the maximum.
📝 Complexity Analysis:
Time Complexity:
O(n²)Space Complexity:
O(1)
✅ Why This Approach?
Simple to implement for small arrays. Avoids complex data structures.
Note: This approach results in Time Limit Exceeded (TLE) for large inputs (fails ~30/1140 test cases due to time constraints).
🆚 Comparison of Approaches
Approach
⏱️ Time Complexity
🗂️ Space Complexity
✅ Pros
⚠️ Cons
Bitmask Greedy
🟢 O(31n)
🟡 O(n)
Balanced speed & memory
Complex bit manipulation
Trie-based
🟢 O(32n)
🟡 O(32n)
Optimal for large datasets
Higher memory usage
Brute-force
🔴 O(n²)
🟢 O(1)
Simple implementation
TLE for large inputs Impractical for large n
✅ Best Choice?
Large Arrays: Use Bitmask Greedy or Trie-based.
Small Arrays (n ≤ 1e3): Brute-force is acceptable.
Code (Java)
Code (Python)
Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐
📍Visitor Count
Last updated