πDay 2. 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.
π Example Walkthrough:
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.
π Solution Code
Code (C++)
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