<p>Given a <strong>(0-indexed)</strong> integer array <code>nums</code> and two integers <code>low</code> and <code>high</code>, return <em>the number of <strong>nice pairs</strong></em>.</p>

<p>A <strong>nice pair</strong> is a pair <code>(i, j)</code> where <code>0 &lt;= i &lt; j &lt; nums.length</code> and <code>low &lt;= (nums[i] XOR nums[j]) &lt;= high</code>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,4,2,7], low = 2, high = 6
<strong>Output:</strong> 6
<strong>Explanation:</strong> All nice pairs (i, j) are as follows:
    - (0, 1): nums[0] XOR nums[1] = 5 
    - (0, 2): nums[0] XOR nums[2] = 3
    - (0, 3): nums[0] XOR nums[3] = 6
    - (1, 2): nums[1] XOR nums[2] = 6
    - (1, 3): nums[1] XOR nums[3] = 3
    - (2, 3): nums[2] XOR nums[3] = 5
</pre>

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

<pre>
<strong>Input:</strong> nums = [9,8,4,2,1], low = 5, high = 14
<strong>Output:</strong> 8
<strong>Explanation:</strong> All nice pairs (i, j) are as follows:
​​​​​    - (0, 2): nums[0] XOR nums[2] = 13
&nbsp;   - (0, 3): nums[0] XOR nums[3] = 11
&nbsp;   - (0, 4): nums[0] XOR nums[4] = 8
&nbsp;   - (1, 2): nums[1] XOR nums[2] = 12
&nbsp;   - (1, 3): nums[1] XOR nums[3] = 10
&nbsp;   - (1, 4): nums[1] XOR nums[4] = 9
&nbsp;   - (2, 3): nums[2] XOR nums[3] = 6
&nbsp;   - (2, 4): nums[2] XOR nums[4] = 5</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= low &lt;= high &lt;= 2 * 10<sup>4</sup></code></li>
</ul>
class TrieNode:
    def __init__(self):
        self.children = defaultdict(TrieNode)
        self.count = 0
        
        
class Trie:
    def __init__(self, maxbits=None):
        self.root = TrieNode()
        self.maxbits = maxbits
        
    def add(self, v):
        n = self.root
        # build the trie from the left-most bit
        for i in reversed(range(self.maxbits)):
            node_value = (v >> i) & 1
            n.children[node_value].count += 1
            n = n.children[node_value]
            
    def count(self, v, high):
        cnt = 0
        n = self.root
        for i in reversed(range(self.maxbits)):
            if not n: break
                
            high_at_bit_i = (high >> i) & 1
            v_at_bit_i = (v >> i) & 1
            if high_at_bit_i:  # x could be v_at_bit_i, and possibly 1-v_at_bit_i
                if v_at_bit_i in n.children:
                    cnt += n.children[v_at_bit_i].count
                n = n.children.get(1 - v_at_bit_i, None)
            else:  # x must be v_at_bit_i
                n = n.children.get(v_at_bit_i, None)
                
        return cnt
                

class Solution:
    def countPairs(self, nums: List[int], low: int, high: int) -> int:
        # determine the max number of bits needed in our trie,
        #    the # of bits for the largest number in nums and high+1, whichever is the greatest
        max_bits = len(bin(max(nums + [high+1]))) - 2
        
        ans = 0
        trie = Trie(max_bits)
        for num in nums:
            ans += trie.count(num, high+1) - trie.count(num, low)  # due to `low <= (nums[i] XOR nums[j]) <= high`
            trie.add(num)
                        
        return ans
28000