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;
}
};
-
2:40:49
FreshandFit
8 hours ago1 Conservative VS Miami Girls
115K61 -
2:28:04
Badlands Media
9 hours agoDevolution Power Hour Ep. 370: Epstein Optics, Russiagate Criminal Referrals, and the Taiwan Countdown
249K30 -
2:02:19
Inverted World Live
8 hours agoThe Epstein Case Closed and the Sky Has Been Weaponized | Ep. 72
61.6K25 -
3:02:27
TimcastIRL
9 hours agoTrump Admin To BAN China Buying U.S. Farmland Citing National Security | Timcast IRL
275K94 -
2:47:48
Laura Loomer
7 hours agoEP132: Blondi Botches Epstein File Release
69.9K130 -
3:41:41
Barry Cunningham
10 hours agoPRESIDENT TRUMP IS STILL MAGA STRONG! ARE YOU?
102K109 -
28:41
Side Scrollers Podcast
8 hours ago$750 Content Creator Trivia Game Show
35.4K1 -
1:37:24
Badlands Media
21 hours agoAltered State S3 Ep. 36: Epstein Disillusion, FISA-Gate, and the Grok AI Uprising
76.4K17 -
3:37:15
I_Came_With_Fire_Podcast
17 hours agoNO EPSTEIN FILES | AI Marco Rubio | China Attacks Germany | Cloud Seeding in TX & NM?
37K11 -
3:14:05
Geeks + Gamers
8 hours agoGeeks + Gamers Play- MARIO KART WORLD
48.6K