<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. A pair of integers <code>x</code> and <code>y</code> is called a <strong>strong</strong> pair if it satisfies the condition:</p>

<ul>
	<li><code>|x - y| &lt;= min(x, y)</code></li>
</ul>

<p>You need to select two integers from <code>nums</code> such that they form a strong pair and their bitwise <code>XOR</code> is the <strong>maximum</strong> among all strong pairs in the array.</p>

<p>Return <em>the <strong>maximum</strong> </em><code>XOR</code><em> value out of all possible strong pairs in the array</em> <code>nums</code>.</p>

<p><strong>Note</strong> that you can pick the same integer twice to form a pair.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4,5]
<strong>Output:</strong> 7
<strong>Explanation:</strong> There are 11 strong pairs in the array <code>nums</code>: (1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) and (5, 5).
The maximum XOR possible from these pairs is 3 XOR 4 = 7.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [10,100]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are 2 strong pairs in the array nums: (10, 10) and (100, 100).
The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [500,520,2500,3000]
<strong>Output:</strong> 1020
<strong>Explanation:</strong> There are 6 strong pairs in the array nums: (500, 500), (500, 520), (520, 520), (2500, 2500), (2500, 3000) and (3000, 3000).
The maximum XOR possible from these pairs is 500 XOR 520 = 1020 since the only other non-zero XOR value is 2500 XOR 3000 = 636.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 2<sup>20</sup> - 1</code></li>
</ul>

class TrieNode:
    def __init__(self):
        self.children = dict()
        self.val = 0 

class Trie:
    def __init__(self, n):
        self.root = TrieNode()
        self.n = n
        
    def add_num(self, num):
        node = self.root 
        for shift in range(self.n, -1, -1): 
            val = 1 if num & (1 << shift) else 0 
            if val not in node.children:
                node.children[val] = TrieNode()
            node = node.children[val]
        node.val = num
        
    def delete(self, num):
        return self._delete(num, self.run_encode(num), 0, self.root)
    
    def run_encode(self, num):
        arr = []
        for shift in range(self.n, -1, -1): 
            val = 1 if num & (1 << shift) else 0 
            arr.append(val)
        return arr
    
    def _delete(self, num, encode, i, current):
        if i == self.n+1:
            if current.val != num:
                return False
            current.val = 0

            # if there is no children, then we could remove this tree node
            return len(current.children) == 0
        
        # get the next node from the encode array
        b = encode[i]
        if b not in current.children:
            return False
        next_node = current.children[b]

        # get the delete signal from the children result
        need_del = self._delete(num, encode, i+1, next_node)
        if need_del:
            del current.children[b]

            # if there is no children, then we could remove this tree node
            return len(current.children) == 0

        return False

class Solution:
    def maximumStrongPairXor(self, nums: List[int]) -> int:
        nums.sort()
        # The max size of value is 2*20
        node = Trie(21)

        # Try to record the available num
        queue = collections.deque([])

        ans = 0
        for num in nums:
            # If the num is too small, it will not satisfy the Strong Pair Condition
            # then we remove it from queue
            while queue and abs(num - queue[0]) > min(num, queue[0]):
                old = queue.popleft()
                # Update the tree
                node.delete(old)
            
            # Update the tree and queue
            node.add_num(num)
            queue.append(num)
            
            cur = node.root
            # Try to find the max answer from the tree
            for shift in range(21, -1, -1):
                val = 1 if num & (1 << shift) else 0
                cur = cur.children[1-val] if 1-val in cur.children else cur.children[val]
            ans = max(ans, num ^ cur.val)
            
        return ans
84700