Skip to content

Commit 73964b9

Browse files
Added line breaks and empty vector test
1 parent abc5e93 commit 73964b9

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

bit_manipulation/max_xor_bit_trie.cpp

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
* each number in the array.
1212
*
1313
* 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-
*
17-
* @author [Abhiraj Mandal](https://github.com/DataWorshipper)
14+
* value in the array (64-bit integers here)
15+
*
16+
* Space Complexity: O(n * log(MAX_VAL))
17+
*
18+
*@author [Abhiraj Mandal](https://github.com/DataWorshipper)
1819
*/
1920

2021
#include <algorithm> // for std::max
@@ -95,7 +96,7 @@ class Trie {
9596
* @param nums vector of unsigned 64-bit integers
9697
* @return maximum XOR of any pair
9798
*/
98-
std::uint64_t findMaximumXOR(const std::vector<std::uint64_t>& nums) {
99+
std::uint64_t find_maximum_xor(const std::vector<std::uint64_t>& nums) {
99100
if (nums.empty()) {
100101
return 0;
101102
}
@@ -117,77 +118,77 @@ std::uint64_t findMaximumXOR(const std::vector<std::uint64_t>& nums) {
117118
* @brief Self-test implementations
118119
*/
119120
static void test() {
120-
using bit_manipulation::max_xor_bit_trie::findMaximumXOR;
121-
121+
using bit_manipulation::max_xor_bit_trie::find_maximum_xor;
122+
122123
// Test 1: LeetCode Example
123124
{
124125
std::vector<std::uint64_t> nums = {3ULL, 10ULL, 5ULL,
125126
25ULL, 2ULL, 8ULL};
126-
assert(findMaximumXOR(nums) == 28ULL);
127+
assert(find_maximum_xor(nums) == 28ULL);
127128
}
128129

129130
// Test 2: Single element
130131
{
131132
std::vector<std::uint64_t> nums = {42ULL};
132-
assert(findMaximumXOR(nums) == 0ULL);
133+
assert(find_maximum_xor(nums) == 0ULL);
133134
}
134135

135136
// Test 3: Two elements
136137
{
137138
std::vector<std::uint64_t> nums = {8ULL, 1ULL};
138-
assert(findMaximumXOR(nums) == 9ULL);
139+
assert(find_maximum_xor(nums) == 9ULL);
139140
}
140141

141142
// Test 4: All zeros
142143
{
143144
std::vector<std::uint64_t> nums = {0ULL, 0ULL, 0ULL};
144-
assert(findMaximumXOR(nums) == 0ULL);
145+
assert(find_maximum_xor(nums) == 0ULL);
145146
}
146147

147148
// Test 5: Max and Min values
148149
{
149150
std::vector<std::uint64_t> nums = {0xFFFFFFFFFFFFFFFFULL,
150151
0x0000000000000000ULL};
151-
assert(findMaximumXOR(nums) == 0xFFFFFFFFFFFFFFFFULL);
152+
assert(find_maximum_xor(nums) == 0xFFFFFFFFFFFFFFFFULL);
152153
}
153154

154155
// Test 6: Duplicates
155156
{
156157
std::vector<std::uint64_t> nums = {7ULL, 7ULL, 7ULL};
157-
assert(findMaximumXOR(nums) == 0ULL);
158+
assert(find_maximum_xor(nums) == 0ULL);
158159
}
159160

160161
// Test 7: Increasing sequence
161162
{
162163
std::vector<std::uint64_t> nums = {1ULL, 2ULL, 3ULL, 4ULL, 5ULL};
163-
assert(findMaximumXOR(nums) == 7ULL);
164+
assert(find_maximum_xor(nums) == 7ULL);
164165
}
165166

166167
// Test 8: Decreasing sequence
167168
{
168169
std::vector<std::uint64_t> nums = {16ULL, 8ULL, 4ULL, 2ULL, 1ULL};
169-
assert(findMaximumXOR(nums) == 24ULL);
170+
assert(find_maximum_xor(nums) == 24ULL);
170171
}
171172

172173
// Test 9: Powers of 2
173174
{
174175
std::vector<std::uint64_t> nums = {1ULL, 2ULL, 4ULL,
175176
8ULL, 16ULL, 32ULL};
176-
assert(findMaximumXOR(nums) == 48ULL);
177+
assert(find_maximum_xor(nums) == 48ULL);
177178
}
178179

179180
// Test 10: Mixed random values
180181
{
181182
std::vector<std::uint64_t> nums = {9ULL, 14ULL, 3ULL, 6ULL, 12ULL};
182-
assert(findMaximumXOR(nums) == 11ULL || findMaximumXOR(nums) == 10ULL ||
183+
assert(find_maximum_xor(nums) == 11ULL || find_maximum_xor(nums) == 10ULL ||
183184
true);
184185
}
185186

186187
// Test 11: Small alternating bits
187188
{
188189
std::vector<std::uint64_t> nums = {0b101010ULL, 0b010101ULL,
189190
0b111111ULL, 0b000000ULL};
190-
assert(findMaximumXOR(nums) == 63ULL);
191+
assert(find_maximum_xor(nums) == 63ULL);
191192
}
192193

193194
// Test 12: Large count
@@ -196,9 +197,15 @@ static void test() {
196197
for (std::uint64_t i = 0; i < 100ULL; ++i) {
197198
nums.push_back(i);
198199
}
199-
assert(findMaximumXOR(nums) > 0ULL);
200+
assert(find_maximum_xor(nums) > 0ULL);
200201
}
201202

203+
// Test 13: Empty Vector Test
204+
{ std::vector<std::uint64_t> nums = {};
205+
206+
assert(find_maximum_xor(nums) == 0ULL);
207+
}
208+
202209
std::cout << "All test cases successfully passed!" << std::endl;
203210
}
204211

0 commit comments

Comments
 (0)