<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>

from typing import List
from collections import deque

class TrieNode:
    def __init__(self):
        self.children = {}

class Trie:
    def __init__(self):
        self.root = TrieNode()
    
    def insert(self, num):
        node = self.root
        opp_node = self.root
        xor = 0
        for i in range(20):
            val = num[i]
            if val not in node.children:
                node.children[val] = (TrieNode(), 0)
            node.children[val] = (node.children[val][0], node.children[val][1] + 1)
            
            opp_val = '1' if val == '0' else '0' 

            if opp_val in opp_node.children:
                opp_node = opp_node.children[opp_val][0]
                xor = xor ^ (1 << (19 - i))
            else:
                opp_node = opp_node.children[val][0]

            node = node.children[val][0]

        return xor
    
    def remove(self, num):
        node = self.root
        for i in range(20):
            val = num[i]
            node.children[val] = (node.children[val][0], node.children[val][1] - 1)
            if node.children[val][1] == 0:
                del node.children[val]
                return
            node = node.children[val][0]

class Solution:
    def maximumStrongPairXor(self, nums: List[int]) -> int:
        nums.sort()
        q = deque()
        trie = Trie()
        ans = 0
        
        for num in nums:
            while len(q) > 0 and num > 2 * q[0]:
                trie.remove(format(q[0], '020b'))
                q.popleft()
            q.append(num)
            ans = max(ans, trie.insert(format(num, '020b')))
        
        return ans

# Explanation of the approach:
# 1. **Sorting**: First, sort the array. This helps in efficiently finding pairs that satisfy the strong pair condition.
# 2. **Strong Pair Condition**: For a pair (x, y) to be strong, |x - y| <= min(x, y). In a sorted array, this translates to y <= 2 * x.
# 3. **Trie for Maximum XOR**: Use a Trie to efficiently calculate the maximum XOR for the current number with previously considered numbers.
# 4. **Sliding Window with Deque**: Use a deque to maintain a window of numbers that can form strong pairs with the current number.
# 5. **Insert and Remove in Trie**: As we iterate through the sorted numbers, remove numbers from the Trie that are no longer valid for forming strong pairs with the current number, and insert the current number into the Trie.
# 6. **Calculate Maximum XOR**: For each number, calculate the maximum XOR with numbers in the Trie and update the answer.

# The time complexity is O(n) due to the efficient use of the Trie and deque, and the space complexity is also O(n) for storing numbers in the Trie.
93200