> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Count the Number of Fair Pairs Python Solution

> Tested Python solution for LeetCode 2563 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 2563, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers), [Binary Search](/catalog/topics/binary-search), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/count-the-number-of-fair-pairs/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2563   # by problem number
lcpy gen -s count_the_number_of_fair_pairs   # by problem name
```

## Problem

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 \<strong>fair pairs\</strong>\</em>.

A pair \<code>(i, j)\</code> is \<strong>fair\</strong> if:

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

### Examples

```
Input: nums = [0,1,7,4,4,5], lower = 3, upper = 6
Output: 6
```

**Explanation:** There are 6 fair pairs: (0,3), (0,4), (0,5), (1,3), (1,4), and (1,5).

```
Input: nums = [1,7,9,2,5], lower = 11, upper = 11
Output: 1
```

**Explanation:** There is a single fair pair: (2,3).

### Constraints

* 1 \<= nums.length \<= 10^5
* nums.length == n
* -10^9 \<= nums\[i] \<= 10^9
* -10^9 \<= lower \<= upper \<= 10^9

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/count_the_number_of_fair_pairs/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/count_the_number_of_fair_pairs/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n)
    # Space: O(n) for the sort
    def count_fair_pairs(self, nums: list[int], lower: int, upper: int) -> int:
        nums.sort()

        def count_at_most(bound: int) -> int:
            left, right = 0, len(nums) - 1
            total = 0
            while left < right:
                if nums[left] + nums[right] <= bound:
                    total += right - left
                    left += 1
                else:
                    right -= 1
            return total

        return count_at_most(upper) - count_at_most(lower - 1)
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) | O(n) for the sort |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.