LeetCode 2563, Medium. Topics: Array, Two Pointers, Binary Search, Sorting. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 20 parametrized pytest cases, and a playground notebook:
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
Explanation: There are 6 fair pairs: (0,3), (0,4), (0,5), (1,3), (1,4), and (1,5).
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, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026