Premium Only Content

2563. Count the Number of Fair Pairs
Given a 0-indexed integer array nums of size n and two integers lower and upper, return the number of fair pairs.
A pair (i, j) is fair if:
0 <= i < j < n, and
lower <= nums[i] + nums[j] <= upper
Example 1:
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).
Example 2:
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 <= 105
nums.length == n
-109 <= nums[i] <= 109
-109 <= lower <= upper <= 109
class Solution {
public:
long long countFairPairs(vector<int>& nums, int lower, int upper) {
ranges::sort(nums);
return countLess(nums, upper) - countLess(nums, lower - 1);
}
private:
long countLess(const vector<int>& nums, int sum) {
long res = 0;
for (int i = 0, j = nums.size() - 1; i < j; ++i) {
while (i < j && nums[i] + nums[j] > sum)
--j;
res += j - i;
}
return res;
}
};
-
1:07:22
Dr. Drew
3 hours agoTargeted: Political Violence Began Long Before Charlie Kirk's Assassination – Leftist Influencers Like Hasan Piker Are Making It Worse w/ Dave Rubin & Jack Posobiec – Ask Dr. Drew
4.01K1 -
1:19:08
TheSaltyCracker
2 hours agoSaltCast 9-25-25
22K41 -
LIVE
StoneMountain64
1 hour agoBattlefield 6 News and Extraction Gaming
191 watching -
24:15
Stephen Gardner
1 hour agoCharlie Kirk Bombshell: Fresh Video Footage Shatters the Narrative!😱
10.1K55 -
9:51
Silver Dragons
2 hours agoSILVER PRICE SOARING - Is Silver In the END GAME Now?
5.48K3 -
1:28:13
Russell Brand
4 hours agoThe Truth About UK Corruption – Andrew Bridgen Speaks Out - SF640
157K35 -
2:11:52
Side Scrollers Podcast
4 hours agoYouTube ADMITS BLATANT Censorship + California Wants to FINE “Hate Speech” + More | Side Scrollers
45.2K2 -
15:26
Simply Bitcoin
1 day ago $0.86 earned$60 Trillion Incoming? | Why Gold & Bonds are Screaming "BUY BITCOIN"
11.4K3 -
1:55:59
The Charlie Kirk Show
4 hours agoDebunking the Lies and Smears about Charlie | ThoughtCrime Team | 9.25.2025
101K63 -
1:05:27
Sean Unpaved
3 hours agoCup Swings, Homer Kings, & TNF Zzzz's
26.7K2