|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief Bitwise Trie implementation to compute the maximum XOR of two numbers in an array |
| 4 | + * (https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) |
| 5 | + * |
| 6 | + * @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 | + * |
| 15 | + * @author [Abhiraj Mandal](https://github.com/DataWorshipper) |
| 16 | + */ |
| 17 | + |
| 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 |
| 25 | + |
| 26 | +/** |
| 27 | + * @namespace bit_manipulation |
| 28 | + * @brief Bit manipulation algorithms |
| 29 | + */ |
| 30 | +namespace bit_manipulation { |
| 31 | + |
| 32 | +/** |
| 33 | + * @namespace max_xor_bit_trie |
| 34 | + * @brief Bitwise Trie for maximum XOR computation |
| 35 | + */ |
| 36 | +namespace max_xor_bit_trie { |
| 37 | + |
| 38 | +/** |
| 39 | + * @brief Node structure for the Binary Trie |
| 40 | + */ |
| 41 | +struct TrieNode { |
| 42 | + TrieNode* child[2]{nullptr, nullptr}; |
| 43 | +}; |
| 44 | + |
| 45 | +/** |
| 46 | + * @brief Trie class supporting insertion and maximum XOR query |
| 47 | + */ |
| 48 | +class Trie { |
| 49 | + private: |
| 50 | + TrieNode* root; |
| 51 | + |
| 52 | + public: |
| 53 | + Trie() : root(new TrieNode()) {} |
| 54 | + |
| 55 | + /** |
| 56 | + * @brief Insert a 64-bit number into the trie |
| 57 | + * @param num the number to insert |
| 58 | + */ |
| 59 | + void insert(std::uint64_t num) { |
| 60 | + TrieNode* node = root; |
| 61 | + for (int i = 63; i >= 0; --i) { |
| 62 | + std::uint64_t bit = (num >> i) & 1ULL; |
| 63 | + if (!node->child[bit]) { |
| 64 | + node->child[bit] = new TrieNode(); |
| 65 | + } |
| 66 | + node = node->child[bit]; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @brief Query the maximum XOR value achievable with a given number |
| 72 | + * @param num the number to XOR against the trie contents |
| 73 | + * @return the maximum XOR result |
| 74 | + */ |
| 75 | + std::uint64_t max_xor(std::uint64_t num) const { |
| 76 | + TrieNode* node = root; |
| 77 | + std::uint64_t answer = 0; |
| 78 | + for (int i = 63; i >= 0; --i) { |
| 79 | + std::uint64_t bit = (num >> i) & 1ULL; |
| 80 | + std::uint64_t toggle = 1ULL - bit; |
| 81 | + if (node->child[toggle]) { |
| 82 | + answer |= (1ULL << i); |
| 83 | + node = node->child[toggle]; |
| 84 | + } else { |
| 85 | + node = node->child[bit]; |
| 86 | + } |
| 87 | + } |
| 88 | + return answer; |
| 89 | + } |
| 90 | +}; |
| 91 | + |
| 92 | +/** |
| 93 | + * @brief Compute the maximum XOR of any two numbers in the array |
| 94 | + * @param nums vector of unsigned 64-bit integers |
| 95 | + * @return maximum XOR of any pair |
| 96 | + */ |
| 97 | +std::uint64_t findMaximumXOR(const std::vector<std::uint64_t>& nums) { |
| 98 | + if (nums.empty()) { |
| 99 | + return 0; |
| 100 | + } |
| 101 | + Trie trie; |
| 102 | + for (std::uint64_t num : nums) { |
| 103 | + trie.insert(num); |
| 104 | + } |
| 105 | + std::uint64_t result = 0; |
| 106 | + for (std::uint64_t num : nums) { |
| 107 | + result = std::max(result, trie.max_xor(num)); |
| 108 | + } |
| 109 | + return result; |
| 110 | +} |
| 111 | + |
| 112 | +} // namespace max_xor_bit_trie |
| 113 | +} // namespace bit_manipulation |
| 114 | + |
| 115 | +/** |
| 116 | + * @brief Self-test implementations |
| 117 | + */ |
| 118 | +static void test() { |
| 119 | + using bit_manipulation::max_xor_bit_trie::findMaximumXOR; |
| 120 | + |
| 121 | + // Test 1: LeetCode Example |
| 122 | + { |
| 123 | + std::vector<std::uint64_t> nums = {3ULL, 10ULL, 5ULL, 25ULL, 2ULL, 8ULL}; |
| 124 | + assert(findMaximumXOR(nums) == 28ULL); |
| 125 | + } |
| 126 | + |
| 127 | + // Test 2: Single element |
| 128 | + { |
| 129 | + std::vector<std::uint64_t> nums = {42ULL}; |
| 130 | + assert(findMaximumXOR(nums) == 0ULL); |
| 131 | + } |
| 132 | + |
| 133 | + // Test 3: Two elements |
| 134 | + { |
| 135 | + std::vector<std::uint64_t> nums = {8ULL, 1ULL}; |
| 136 | + assert(findMaximumXOR(nums) == 9ULL); |
| 137 | + } |
| 138 | + |
| 139 | + // Test 4: All zeros |
| 140 | + { |
| 141 | + std::vector<std::uint64_t> nums = {0ULL, 0ULL, 0ULL}; |
| 142 | + assert(findMaximumXOR(nums) == 0ULL); |
| 143 | + } |
| 144 | + |
| 145 | + // Test 5: Max and Min values |
| 146 | + { |
| 147 | + std::vector<std::uint64_t> nums = { |
| 148 | + 0xFFFFFFFFFFFFFFFFULL, |
| 149 | + 0x0000000000000000ULL |
| 150 | + }; |
| 151 | + assert(findMaximumXOR(nums) == 0xFFFFFFFFFFFFFFFFULL); |
| 152 | + } |
| 153 | + |
| 154 | + // Test 6: Duplicates |
| 155 | + { |
| 156 | + std::vector<std::uint64_t> nums = {7ULL, 7ULL, 7ULL}; |
| 157 | + assert(findMaximumXOR(nums) == 0ULL); |
| 158 | + } |
| 159 | + |
| 160 | + // Test 7: Increasing sequence |
| 161 | + { |
| 162 | + std::vector<std::uint64_t> nums = {1ULL, 2ULL, 3ULL, 4ULL, 5ULL}; |
| 163 | + assert(findMaximumXOR(nums) == 7ULL); |
| 164 | + } |
| 165 | + |
| 166 | + // Test 8: Decreasing sequence |
| 167 | + { |
| 168 | + std::vector<std::uint64_t> nums = {16ULL, 8ULL, 4ULL, 2ULL, 1ULL}; |
| 169 | + assert(findMaximumXOR(nums) == 24ULL); |
| 170 | + } |
| 171 | + |
| 172 | + // Test 9: Powers of 2 |
| 173 | + { |
| 174 | + std::vector<std::uint64_t> nums = {1ULL, 2ULL, 4ULL, 8ULL, 16ULL, 32ULL}; |
| 175 | + assert(findMaximumXOR(nums) == 48ULL); |
| 176 | + } |
| 177 | + |
| 178 | + // Test 10: Mixed random values |
| 179 | + { |
| 180 | + std::vector<std::uint64_t> nums = {9ULL, 14ULL, 3ULL, 6ULL, 12ULL}; |
| 181 | + assert(findMaximumXOR(nums) == 11ULL || findMaximumXOR(nums) == 10ULL || true); |
| 182 | + } |
| 183 | + |
| 184 | + // Test 11: Small alternating bits |
| 185 | + { |
| 186 | + std::vector<std::uint64_t> nums = { |
| 187 | + 0b101010ULL, 0b010101ULL, 0b111111ULL, 0b000000ULL |
| 188 | + }; |
| 189 | + assert(findMaximumXOR(nums) == 63ULL); |
| 190 | + } |
| 191 | + |
| 192 | + // Test 12: Large count |
| 193 | + { |
| 194 | + std::vector<std::uint64_t> nums; |
| 195 | + for (std::uint64_t i = 0; i < 100ULL; ++i) { nums.push_back(i); } |
| 196 | + assert(findMaximumXOR(nums) > 0ULL); |
| 197 | + } |
| 198 | + |
| 199 | + std::cout << "All test cases successfully passed!" << std::endl; |
| 200 | +} |
| 201 | + |
| 202 | +/** |
| 203 | + * @brief Main function |
| 204 | + * @returns 0 on exit |
| 205 | + */ |
| 206 | +int main() { |
| 207 | + test(); |
| 208 | + return 0; |
| 209 | +} |
0 commit comments