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;
}
};
-
3:41:41
Barry Cunningham
7 hours agoPRESIDENT TRUMP IS STILL MAGA STRONG! ARE YOU?
94.1K87 -
28:41
Side Scrollers Podcast
6 hours ago$750 Content Creator Trivia Game Show
27.8K1 -
1:37:24
Badlands Media
19 hours agoAltered State S3 Ep. 36: Epstein Disillusion, FISA-Gate, and the Grok AI Uprising
68.5K16 -
3:37:15
I_Came_With_Fire_Podcast
15 hours agoNO EPSTEIN FILES | AI Marco Rubio | China Attacks Germany | Cloud Seeding in TX & NM?
30.4K10 -
3:14:05
Geeks + Gamers
6 hours agoGeeks + Gamers Play- MARIO KART WORLD
42.7K -
4:22:24
Alex Zedra
5 hours agoLIVE! Scary Games tn!
37.6K2 -
47:26
Friday Beers
8 hours ago $1.68 earnedWe Drank 12 Beers & Found the Zodiac Killer
37.8K2 -
1:02:33
Mike Rowe
1 day agoCan We Fix The American Diet? | Jillian Michaels #441 | The Way I Heard It
23.8K17 -
1:08:23
VapinGamers
6 hours ago $1.15 earnedDune Awakening - Community Spice and Everything Nice Game Night - !rumbot !music
18.4K -
3:32:02
Fragniac
7 hours ago🔴 MAKE FPS GREAT AGAIN❗
15.8K1