This problem looks busy at first, but the accepted solution is built around one steady invariant. For 421. Maximum XOR of Two Numbers in an Array, the solution in this repository is mainly a bit manipulation solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: bit manipulation, greedy.
The notes already sitting in the source point us in the right direction:
- TLE
- 28 / 29 test cases passed.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are findMaximumXOR.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- A set is doing the membership or uniqueness work, which keeps the main loop readable.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//TLE
02//28 / 29 test cases passed.
03class Solution {
04public:
05 int findMaximumXOR(vector<int>& nums) {
06 sort(nums.begin(), nums.end());
07
08 int ans = 0;
09 for(int i = 0; i < nums.size()-1; i++){
10 for(int j = i+1; j < nums.size(); j++){
11 ans = max(ans, nums[i]^nums[j]);
12 }
13 }
14 return ans;
15 }
16};
17
18//https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/discuss/91049/Java-O(n)-solution-using-bit-manipulation-and-HashMap
19//Runtime: 344 ms, faster than 5.11% of C++ online submissions for Maximum XOR of Two Numbers in an Array.
20//Memory Usage: 38 MB, less than 80.00% of C++ online submissions for Maximum XOR of Two Numbers in an Array.
21class Solution {
22public:
23 int findMaximumXOR(vector<int>& nums) {
24 int ans = 0;
25 int mask = 0;
26
27 for(int i = 31; i >= 0; i--){
28 //100..00(it has 31 0s ), 1100..00, ..., 1..10(31 1s), 1..1(32 1s)
29 mask |= (1 << i);
30
31 set<int> leftPartOfNums;
32
33 for(int num : nums){
34 leftPartOfNums.insert(num & mask);
35 }
36
37 //if ans is 1100, and i is 1 now, we wish we can make ans 1110
38 int greedyTry = ans | (1 << i);
39
40// if(i <= 5){
41// cout << "mask: " << endl;
42// cout << bitset<32>(mask).to_string() << endl;
43
44// cout << "greedyTry: " << endl;
45// cout << bitset<32>(greedyTry).to_string() << endl;
46// }
47
48 for(int leftPartOfNum : leftPartOfNums){
49 /*
50 we wish there is a "anotherNum" s.t.
51 leftPartOfNum ^ anotherNum = greedyTry,
52 we can calculate anotherNum by leftPartOfNum ^ greedyTry,
53 then we need to check if it exists in the set
54 */
55 int anotherNum = leftPartOfNum ^ greedyTry;
56
57 // if(i <= 5){
58 // cout << "anotherNum: " << endl;
59 // cout << bitset<32>(anotherNum).to_string() << endl;
60 // }
61
62 if(leftPartOfNums.find(anotherNum) != leftPartOfNums.end()){
63 /*
64 we can find a anotherNum s.t.
65 leftPartOfNum ^ anotherNum =
66 leftPartOfNum ^ leftPartOfNum ^ greedyTry =
67 greedyTry
68 */
69 ans = greedyTry;
70 break;
71 }
72 }
73
74 }
75 return ans;
76 }
77};
Cost