<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 Solution:
    def countPairs(self, nums, low, high):
        nums.sort()
        return self.countWithinGroup(nums, high) - self.countWithinGroup(nums, low - 1)

    def binarySearch(self, group, left, right, bit):
        while left < right:
            mid = (left + right) // 2
            if (group[mid] >> bit) & 1:
                right = mid
            else:
                left = mid + 1
        return left

    def countBetweenGroups(self, group, high, left0, right0, left1, right1, bit):
        if left0 == right0 or left1 == right1 or bit == -1:
            return (right0 - left0) * (right1 - left1)
        
        mid0 = self.binarySearch(group, left0, right0, bit)
        mid1 = self.binarySearch(group, left1, right1, bit)
        
        if (high >> bit) & 1:
            return (
                (mid0 - left0) * (mid1 - left1)
                + (right0 - mid0) * (right1 - mid1)
                + self.countBetweenGroups(group, high, left0, mid0, mid1, right1, bit - 1)
                + self.countBetweenGroups(group, high, left1, mid1, mid0, right0, bit - 1)
            )
        else:
            return (
                self.countBetweenGroups(group, high, left0, mid0, left1, mid1, bit - 1)
                + self.countBetweenGroups(group, high, mid0, right0, mid1, right1, bit - 1)
            )

    def countWithinGroup(self, group, high, left=-1, right=-1, bit=14):
        if left == -1:
            left = 0
            right = len(group)
        elif left == right or bit == -1:
            return (right - left) * (right - left - 1) // 2
        
        mid = self.binarySearch(group, left, right, bit)
        
        if (high >> bit) & 1:
            return (
                (mid - left) * (mid - left - 1) // 2
                + (right - mid) * (right - mid - 1) // 2
                + self.countBetweenGroups(group, high, left, mid, mid, right, bit - 1)
            )
        else:
            return (
                self.countWithinGroup(group, high, left, mid, bit - 1)
                + self.countWithinGroup(group, high, mid, right, bit - 1)
            )
18800