JavaScript - Trie / Bit Manipulation with explaination and easy understanding
Intuition:\n\nWe want to find the maximum XOR of any two numbers of this Array. This means bit manipulation is involved here. This forces us to think about the 
**Intuition**:\n\nWe want to find the maximum XOR of any two numbers of this Array. This means bit manipulation is involved here. This forces us to think about the binary representation of array members. In order for any number to have maximum XOR with another number, the other number needs to have opposite bits at all bits position.\n\n(for any bit position x - could be 0 or 1)\n```\n0 ^ x = x\nx ^ x = x\n```\n\nIn order to maximise this number, we start from the most significant bit of the binary number. This gives a hint that prefix calculation might be involved, which means we\'ll be using Trie data structure here. \n\n\n```\nvar findMaximumXOR = function(nums) {\n  let trie = new Trie();\n  let root = trie.root;\n  \n  // build a bitwise trie using all the numbers in nums\n  for(let num of nums) {\n    trie.insert(num);\n  }\n  \n  let ans = 0;\n  let INTEGER_MAX = Math.pow(2, 31) - 1;\n  for(let num of nums) {\n    let target = num ^ INTEGER_MAX;\n    let found = trie.search(target);\n    ans = Math.max(ans, num ^ found);\n  }\n  return ans;\n};\n\nclass Trie {\n  root = new Map();\n  \n  // inserts binary representation of a number\n  insert(number) {\n    // for a 32 bit signed integer, there will be 31 bits.\n    // So we start at 30 and go down to 0. (from MSB to LSB)\n    let index = 30;\n    let node = this.root;\n    while(index >= 0) {\n      // create a mask, for index 30, it\'ll be Math.pow(2, 30)\n      let mask = 1 << index;\n      // get the bit value at current position, Bitwise AND will\n      // be positive if the bit is set, and 0 if it is unset.\n      let bit = (number & mask) > 0 ? 1 : 0;\n      if(!node.has(bit)) {\n        node.set(bit, new Map());\n      }\n      node = node.get(bit);\n      index--;\n    }\n  }\n\n  // tries to find number in Trie, if not, then the closest\n  // possible\n  search(number) {\n    let ans = 0;\n    let index = 30;\n    let node = this.root;\n    while(index >= 0) {\n      let mask = 1 << index;\n      let bit = (number & mask) > 0 ? 1 : 0;\n      // found bit in Trie, proceed further\n      if (node.has(bit)) {\n        node = node.get(bit);\n\n      // bit is not present in Trie, proceed with\n      // what is present there then\n      } else {\n        bit = bit === 0 ? 1 : 0\n        node = node.get(bit);\n      }\n\n      if (bit === 1) {\n        ans = ans + mask;\n      }\n      index--;\n    }\n    return ans;\n  }\n}\n```
['Trie', 'JavaScript']