Maximum Strong Pair XOR II - Bitwise Trie [JS]
Description Solution: Bitwise Trie & Two Pointers Sort nums in asc order. For each nums[i] , move up index j while nums[j] - nums[i] <= nums[i] . Use a bitwise trie to find the maximum bitwise XOR, which is a greedy strategy of taking the opposite bit whenever possible. n = length of nums , m = max(nums[i]) Time Complexity: O(n log(m)) Space Complexity: O(m) var maximumStrongPairXor = function ( nums ) { nums.sort( ( a, b ) => a - b); let n = nums.length, trie = new BitwiseTrie(), maxXor = 0 ; for ( let i = 0 , j = 0 ; i < n; i++) { if (i > 0 ) trie.removeNum(nums[i - 1 ]); while (j < n && nums[j] - nums[i] <= nums[i]) { trie.addNum(nums[j]); j++; } maxXor = Math .max(maxXor, trie.maxXor(nums[i])); } return maxXor; }; class TrieNode { constructor ( ) { this .children = Array ( 2 ); this .num = null ; this .count = 0 ; } } class BitwiseTrie ...