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;
}
};
-
LIVE
Grant Stinchfield
1 hour agoRFK Jr. Speaks Out: The Pain of Assassination & the Loss of Charlie Kirk
78 watching -
1:59:33
Benny Johnson
2 hours agoKash Patel Testifying LIVE Now on Charlie Kirk Assassination, Trump to Declare Antifa TERRORISTS?!
32K48 -
2:07:38
Timcast
3 hours ago🚨LIVE: Kash Patel Testifies Over Charlie Kirk Assassination In Senate | Tim Pool
82.9K54 -
LIVE
Trumpet Daily
52 minutes agoTrumpet Daily LIVE | Sept. 16, 2025
482 watching -
LIVE
The Shannon Joy Show
1 hour agoTrojan Horse Trump Pushing ‘Worse Than Biden’ Speech Control Using Kirk Killing. Guest Brett Miller
234 watching -
1:01:35
VINCE
3 hours agoThe Left's 'Malignant' Violence Problem | Episode 126 - 09/16/25
225K113 -
LIVE
LFA TV
6 hours agoLFA TV ALL DAY STREAM - TUESDAY 9/16/25
4,348 watching -
1:45:59
Dear America
4 hours agoKiller ADMITS To Killing Charlie In DISCORD. Terror Cell EXPOSED! + JD Fills In on Charlie’s Show!
162K85 -
2:59:28
Wendy Bell Radio
7 hours agoThe Left Lives In A Bubble
72K141 -
LIVE
Barry Cunningham
3 hours agoLIVE BREAKING NEWS: KASH PATEL HEARING!
1,631 watching