|
| 1 | +""" |
| 2 | +Author : Abhiraj Mandal |
| 3 | +Date : October 3, 2025 |
| 4 | +
|
| 5 | +This is a pure Python implementation of Bitwise Trie (Binary Trie) to compute |
| 6 | +the maximum XOR of any two numbers in an array. |
| 7 | +
|
| 8 | +The problem is: |
| 9 | +Given an array of integers, find the maximum XOR value obtainable by XOR-ing |
| 10 | +any two numbers in the array. The implementation uses a bitwise Trie to efficiently |
| 11 | +compute the maximum XOR. |
| 12 | +""" |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +class TrieNode: |
| 18 | + """Node of the Bitwise Trie.""" |
| 19 | + def __init__(self) -> None: |
| 20 | + self.child = [None, None] # child[0] for bit 0, child[1] for bit 1 |
| 21 | + |
| 22 | + |
| 23 | +class BitwiseTrieMaxXOR: |
| 24 | + """ |
| 25 | + Use: |
| 26 | + solver = BitwiseTrieMaxXOR() |
| 27 | + result = solver.find_maximum_xor(nums) |
| 28 | + """ |
| 29 | + |
| 30 | + def __init__(self) -> None: |
| 31 | + self.root = TrieNode() |
| 32 | + |
| 33 | + def insert(self, num: int) -> None: |
| 34 | + """Insert a number into the trie.""" |
| 35 | + node = self.root |
| 36 | + for i in range(31, -1, -1): # 32-bit integers |
| 37 | + bit = (num >> i) & 1 |
| 38 | + if not node.child[bit]: |
| 39 | + node.child[bit] = TrieNode() |
| 40 | + node = node.child[bit] |
| 41 | + |
| 42 | + def query_max_xor(self, num: int) -> int: |
| 43 | + """ |
| 44 | + Query the maximum XOR achievable with `num` using the trie. |
| 45 | +
|
| 46 | + Args: |
| 47 | + num (int): The number to XOR against numbers in the trie. |
| 48 | +
|
| 49 | + Returns: |
| 50 | + int: Maximum XOR value achievable with `num`. |
| 51 | + """ |
| 52 | + node = self.root |
| 53 | + max_xor = 0 |
| 54 | + for i in range(31, -1, -1): |
| 55 | + bit = (num >> i) & 1 |
| 56 | + toggle = 1 - bit |
| 57 | + if node.child[toggle]: |
| 58 | + max_xor |= (1 << i) |
| 59 | + node = node.child[toggle] |
| 60 | + else: |
| 61 | + node = node.child[bit] |
| 62 | + return max_xor |
| 63 | + |
| 64 | + def find_maximum_xor(self, nums: list[int]) -> int: |
| 65 | + """Compute maximum XOR of any two numbers in `nums`.""" |
| 66 | + if not nums: |
| 67 | + return 0 |
| 68 | + for num in nums: |
| 69 | + self.insert(num) |
| 70 | + return max(self.query_max_xor(num) for num in nums) |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + print("************ Testing Bitwise Trie Maximum XOR Algorithm ************\n") |
| 75 | + |
| 76 | + test_cases = [ |
| 77 | + ([3, 10, 5, 25, 2, 8], 28), |
| 78 | + ([42], 0), |
| 79 | + ([8, 1], 9), |
| 80 | + ([0, 0, 0], 0), |
| 81 | + ([0xFFFFFFFF, 0], 0xFFFFFFFF), |
| 82 | + ([7, 7, 7], 0), |
| 83 | + ([1, 2, 3, 4, 5], 7), |
| 84 | + ([16, 8, 4, 2, 1], 24), |
| 85 | + ([1, 2, 4, 8, 16, 32], 48), |
| 86 | + ([9, 14, 3, 6, 12], 15), |
| 87 | + ] |
| 88 | + |
| 89 | + for idx, (nums, expected) in enumerate(test_cases, 1): |
| 90 | + solver = BitwiseTrieMaxXOR() # Reset trie for each test case |
| 91 | + result = solver.find_maximum_xor(nums) |
| 92 | + print(f"Testcase {idx}: Expected={expected}, Got={result}") |
| 93 | + assert result == expected, f"Testcase {idx} failed!" |
| 94 | + |
| 95 | + print("\nAll test cases successfully passed!") |
| 96 | + print("********** End of Testing Bitwise Trie Maximum XOR Algorithm **********") |
0 commit comments