<p>Given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code> and two integers <code>lower</code> and <code>upper</code>, return <em>the number of fair pairs</em>.</p>

<p>A pair <code>(i, j)</code> is <b>fair </b>if:</p>

<ul>
	<li><code>0 &lt;= i &lt; j &lt; n</code>, and</li>
	<li><code>lower &lt;= nums[i] + nums[j] &lt;= upper</code></li>
</ul>

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

<pre>
<strong>Input:</strong> nums = [0,1,7,4,4,5], lower = 3, upper = 6
<strong>Output:</strong> 6
<strong>Explanation:</strong> There are 6 fair pairs: (0,3), (0,4), (0,5), (1,3), (1,4), and (1,5).
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,7,9,2,5], lower = 11, upper = 11
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is a single fair pair: (2,3).
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>nums.length == n</code></li>
	<li><code><font face="monospace">-10<sup>9</sup></font>&nbsp;&lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li><code><font face="monospace">-10<sup>9</sup>&nbsp;&lt;= lower &lt;= upper &lt;= 10<sup>9</sup></font></code></li>
</ul>

class Solution:
    def countFairPairs(self, nums: List[int], lower: int, upper: int) -> int:

        nums = sorted(nums)

        res = 0
        for numI in range(len(nums)):
            # print("here")
            num = nums[numI]
            # if num > upper:
            #     break
            tar = lower - num

            upperTar = upper - num

            i = self.bs(tar, nums, numI+1)
            j = self.bsU(upperTar, nums, i)
            res += j-i

        return res
                



    def bs(self, target, arr, l):
        r = len(arr)
        while l < r:
            # print(l, r)
            m = l + (r-l)//2
            mid = arr[m]
            if mid < target:
                l = m+1
            else:
                r = m

        return l
        

    def bsU(self, target, arr, l):
        r = len(arr)
        while l < r:
            # print(l, r)
            m = l + (r-l)//2
            mid = arr[m]
            if mid <= target:
                l = m+1
            else:
                r = m

        return l
1444