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:10:46
Ellie_roe
4 hours agoEllie and Errys Halloween Spooktacular || Random Horror Games
11K -
50:27
Sarah Westall
6 hours agoBig Banks Caught Rigging Market, IMF tells World to “Buckle Up” w/ Andy Schectman
25.3K7 -
13:54
Degenerate Jay
13 hours ago $0.91 earned5 Best Superhero Movies To Watch On Halloween
11.1K4 -
59:03
NAG Podcast
6 hours agoSarah Fields: BOLDTALK W/Angela Belcamino
17.7K5 -
1:21:41
Glenn Greenwald
8 hours agoGlenn Takes Your Questions: On the Argentina Bailout, Money in Politics, and More | SYSTEM UPDATE #541
76.8K38 -
3:10:08
Barry Cunningham
6 hours agoPRESIDENT TRUMP TO USE NUCLEAR OPTION? FOOD STAMPS END! | SHUTDOWN DAY 31
45.2K31 -
1:06:56
BonginoReport
13 hours agoThe Battle Between Good & Evil w/ Demonologist Rick Hansen - Hayley Caronia (Ep.168)
97.9K33 -
1:12:57
Kim Iversen
8 hours agoBill Gates Suddenly Says “Don’t Worry About Climate Change”?
88.3K60 -
1:05:12
Michael Franzese
8 hours agoI Waited 50 Years to Tell You What Happened on Halloween 1975
44K17 -
1:07:15
Candace Show Podcast
8 hours agoINFILTRATION: Charlie Kirk Was Being Tracked For Years. | Candace Ep 256
90.7K351