11/* *
22 * @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
45 * (https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/)
56 *
67 * @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+ *
1517 * @author [Abhiraj Mandal](https://github.com/DataWorshipper)
1618 */
1719
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
2526
2627/* *
2728 * @namespace bit_manipulation
@@ -120,8 +121,9 @@ static void test() {
120121
121122 // Test 1: LeetCode Example
122123 {
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 );
125127 }
126128
127129 // Test 2: Single element
@@ -133,7 +135,7 @@ static void test() {
133135 // Test 3: Two elements
134136 {
135137 std::vector<std::uint64_t > nums = {8ULL , 1ULL };
136- assert (findMaximumXOR (nums) == 9ULL );
138+ assert (findMaximumXOR (nums) == 9ULL );
137139 }
138140
139141 // Test 4: All zeros
@@ -144,10 +146,8 @@ static void test() {
144146
145147 // Test 5: Max and Min values
146148 {
147- std::vector<std::uint64_t > nums = {
148- 0xFFFFFFFFFFFFFFFFULL ,
149- 0x0000000000000000ULL
150- };
149+ std::vector<std::uint64_t > nums = {0xFFFFFFFFFFFFFFFFULL ,
150+ 0x0000000000000000ULL };
151151 assert (findMaximumXOR (nums) == 0xFFFFFFFFFFFFFFFFULL );
152152 }
153153
@@ -160,39 +160,42 @@ static void test() {
160160 // Test 7: Increasing sequence
161161 {
162162 std::vector<std::uint64_t > nums = {1ULL , 2ULL , 3ULL , 4ULL , 5ULL };
163- assert (findMaximumXOR (nums) == 7ULL );
163+ assert (findMaximumXOR (nums) == 7ULL );
164164 }
165165
166166 // Test 8: Decreasing sequence
167167 {
168168 std::vector<std::uint64_t > nums = {16ULL , 8ULL , 4ULL , 2ULL , 1ULL };
169- assert (findMaximumXOR (nums) == 24ULL );
169+ assert (findMaximumXOR (nums) == 24ULL );
170170 }
171171
172172 // Test 9: Powers of 2
173173 {
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 );
176177 }
177178
178179 // Test 10: Mixed random values
179180 {
180181 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 );
182184 }
183185
184186 // Test 11: Small alternating bits
185187 {
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 );
190191 }
191192
192193 // Test 12: Large count
193194 {
194195 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+ }
196199 assert (findMaximumXOR (nums) > 0ULL );
197200 }
198201
0 commit comments