1
1
/* *
2
2
* @file
3
- * @brief Bitwise Trie implementation to compute the maximum XOR of two numbers in an array
3
+ * @brief Bitwise Trie implementation to compute the maximum XOR of two numbers
4
+ * in an array
4
5
* (https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/)
5
6
*
6
7
* @details
7
- * Given an array of n integers, the task is to find the maximum XOR value obtainable
8
- * by XOR-ing any two numbers in the array. This implementation uses a bitwise Trie
9
- * (Binary Trie) to efficiently calculate the maximum XOR for each number in the array.
10
- *
11
- * Worst Case Time Complexity: O(n * log(MAX_VAL)) where MAX_VAL is the maximum value
12
- * in the array (64-bit integers here)
13
- * Space Complexity: O(n * log(MAX_VAL))
14
- *
8
+ * Given an array of n integers, the task is to find the maximum XOR value
9
+ * obtainable by XOR-ing any two numbers in the array. This implementation uses
10
+ * a bitwise Trie (Binary Trie) to efficiently calculate the maximum XOR for
11
+ * each number in the array.
12
+ *
13
+ * Worst Case Time Complexity: O(n * log(MAX_VAL)) where MAX_VAL is the maximum
14
+ * value in the array (64-bit integers here) Space Complexity: O(n *
15
+ * log(MAX_VAL))
16
+ *
15
17
* @author [Abhiraj Mandal](https://github.com/DataWorshipper)
16
18
*/
17
19
18
-
19
- #include < algorithm> // for std::max
20
- #include < cassert> // for assert
21
- #include < cstdint> // for std::uint64_t
22
- #include < limits> // for std::numeric_limits
23
- #include < vector> // for std::vector
24
- #include < iostream> // for std::cout and std::endl
20
+ #include < algorithm> // for std::max
21
+ #include < cassert> // for assert
22
+ #include < cstdint> // for std::uint64_t
23
+ #include < iostream> // for std::cout and std::endl
24
+ #include < limits> // for std::numeric_limits
25
+ #include < vector> // for std::vector
25
26
26
27
/* *
27
28
* @namespace bit_manipulation
@@ -120,8 +121,9 @@ static void test() {
120
121
121
122
// Test 1: LeetCode Example
122
123
{
123
- std::vector<std::uint64_t > nums = {3ULL , 10ULL , 5ULL , 25ULL , 2ULL , 8ULL };
124
- assert (findMaximumXOR (nums) == 28ULL );
124
+ std::vector<std::uint64_t > nums = {3ULL , 10ULL , 5ULL ,
125
+ 25ULL , 2ULL , 8ULL };
126
+ assert (findMaximumXOR (nums) == 28ULL );
125
127
}
126
128
127
129
// Test 2: Single element
@@ -133,7 +135,7 @@ static void test() {
133
135
// Test 3: Two elements
134
136
{
135
137
std::vector<std::uint64_t > nums = {8ULL , 1ULL };
136
- assert (findMaximumXOR (nums) == 9ULL );
138
+ assert (findMaximumXOR (nums) == 9ULL );
137
139
}
138
140
139
141
// Test 4: All zeros
@@ -144,10 +146,8 @@ static void test() {
144
146
145
147
// Test 5: Max and Min values
146
148
{
147
- std::vector<std::uint64_t > nums = {
148
- 0xFFFFFFFFFFFFFFFFULL ,
149
- 0x0000000000000000ULL
150
- };
149
+ std::vector<std::uint64_t > nums = {0xFFFFFFFFFFFFFFFFULL ,
150
+ 0x0000000000000000ULL };
151
151
assert (findMaximumXOR (nums) == 0xFFFFFFFFFFFFFFFFULL );
152
152
}
153
153
@@ -160,39 +160,42 @@ static void test() {
160
160
// Test 7: Increasing sequence
161
161
{
162
162
std::vector<std::uint64_t > nums = {1ULL , 2ULL , 3ULL , 4ULL , 5ULL };
163
- assert (findMaximumXOR (nums) == 7ULL );
163
+ assert (findMaximumXOR (nums) == 7ULL );
164
164
}
165
165
166
166
// Test 8: Decreasing sequence
167
167
{
168
168
std::vector<std::uint64_t > nums = {16ULL , 8ULL , 4ULL , 2ULL , 1ULL };
169
- assert (findMaximumXOR (nums) == 24ULL );
169
+ assert (findMaximumXOR (nums) == 24ULL );
170
170
}
171
171
172
172
// Test 9: Powers of 2
173
173
{
174
- std::vector<std::uint64_t > nums = {1ULL , 2ULL , 4ULL , 8ULL , 16ULL , 32ULL };
175
- assert (findMaximumXOR (nums) == 48ULL );
174
+ std::vector<std::uint64_t > nums = {1ULL , 2ULL , 4ULL ,
175
+ 8ULL , 16ULL , 32ULL };
176
+ assert (findMaximumXOR (nums) == 48ULL );
176
177
}
177
178
178
179
// Test 10: Mixed random values
179
180
{
180
181
std::vector<std::uint64_t > nums = {9ULL , 14ULL , 3ULL , 6ULL , 12ULL };
181
- assert (findMaximumXOR (nums) == 11ULL || findMaximumXOR (nums) == 10ULL || true );
182
+ assert (findMaximumXOR (nums) == 11ULL || findMaximumXOR (nums) == 10ULL ||
183
+ true );
182
184
}
183
185
184
186
// Test 11: Small alternating bits
185
187
{
186
- std::vector<std::uint64_t > nums = {
187
- 0b101010ULL , 0b010101ULL , 0b111111ULL , 0b000000ULL
188
- };
189
- assert (findMaximumXOR (nums) == 63ULL );
188
+ std::vector<std::uint64_t > nums = {0b101010ULL , 0b010101ULL ,
189
+ 0b111111ULL , 0b000000ULL };
190
+ assert (findMaximumXOR (nums) == 63ULL );
190
191
}
191
192
192
193
// Test 12: Large count
193
194
{
194
195
std::vector<std::uint64_t > nums;
195
- for (std::uint64_t i = 0 ; i < 100ULL ; ++i) { nums.push_back (i); }
196
+ for (std::uint64_t i = 0 ; i < 100ULL ; ++i) {
197
+ nums.push_back (i);
198
+ }
196
199
assert (findMaximumXOR (nums) > 0ULL );
197
200
}
198
201
0 commit comments