Clean Python Solution using TRIE with Comments Easy to Understand
Insert all the elements of nums in a trie with bit values in respective position. where every node can have 2 children either with 0 key or 1 key. \n\nAs XOR is
Insert all the elements of nums in a trie with bit values in respective position. where every node can have 2 children either with 0 key or 1 key. \n\nAs XOR is a **inequality detector** so we try to maximize the inequality between num and node. So that the XOR of num and value of node will give the max value.\n\nSo we do the following steps While traversing the num from 31\'th bit position to 0\'th bit position:\n1. If the current bit of num is 1 then we try to move the cur pointer towards the child with 0 key.\n2. And if the current bit of num is 0 then we try to move the cur pointer towards the child with 1 key.\n\n```\n\nclass TrieNode:\n    def __init__(self):\n        self.children = {}\n        self.val = -1           # used to store the value of entire number at the end of trie node\n\nclass Trie:\n    def __init__(self):\n        self.root = TrieNode()  # object of trienode class\n    \n    def addNum(self, num):\n        cur = self.root         # every time start from root\n        for i in range(31, -1, -1):\n            bit = 1 if num & (1 << i) else 0  # bit value i\'th position of num \n            if bit not in cur.children:\n                cur.children[bit] = TrieNode()\n            cur = cur.children[bit]\n        cur.val = num           # storing the value of entire num at the end of trie node\n                \n\nclass Solution:\n    def findMaximumXOR(self, nums: List[int]) -> int:\n        trie = Trie()           # creating object of Trie class\n        for num in nums:        # adding all num to the trie structure\n            trie.addNum(num)\n        \n        res = 0\n        for num in nums:        \n            cur = trie.root     # every time start cur pointer from root\n            for i in range(31, -1, -1):\n                bit = 1 if num & (1 << i) else 0  # bit value of i\'th position of num  \n                if bit == 1:                      # try to move towards opposite key ie. 0\n                    if 0 in cur.children:         # opposit key 0 exist then defenetly go towards the child 0\n                        cur = cur.children[0]\n                    else:                         # opposit key 0 not exist so we have only option to go towards what we have ie. 1\n                        cur = cur.children[1]\n                else:     # bit == 0              # try to move towards opposite key ie. 1\n                    if 1 in cur.children:         # opposit key 1 exist then defenetly go towards the child 1\n                        cur = cur.children[1]\n                    else:                         # opposit key 1 not exist so we have only option to go towards what we have ie. 0\n                        cur = cur.children[0]\n            # as we tried to maximize the inequality between cur.val and num so XOR of them will give max value\n            res = max(res, cur.val ^ num)\n        \n        return res\n```
['Trie', 'Python', 'Python3']