Skip to content

Commit 4ed7cae

Browse files
committed
feat(LeetCode): Solve leetcode/leetcoding_challenge/2025/apr2025/week3/count_the_number_of_fair_pairs.cpp
1 parent bf478f8 commit 4ed7cae

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <algorithm>
2+
#include <vector>
3+
#include <queue>
4+
#include <set>
5+
#include <limits>
6+
#include <map>
7+
#include <unordered_set>
8+
#include <unordered_map>
9+
#include <iterator>
10+
#include <sstream>
11+
#include <iostream> // includes cin to read from stdin and cout to write to stdout
12+
using namespace std; // since cin and cout are both in namespace std, this saves some text
13+
14+
class Solution {
15+
public:
16+
long long countFairPairs(vector<int>& nums, int lower, int upper) {
17+
sort(begin(nums), end(nums));
18+
19+
long long answer = 0;
20+
for (int i = 0; i < nums.size(); ++i) {
21+
auto num = nums[i];
22+
23+
int left = max(i + 1, (int) distance(begin(nums), lower_bound(begin(nums), end(nums), lower - num)));
24+
int right = distance(begin(nums),upper_bound(begin(nums), end(nums), upper - num));
25+
26+
answer += max(0, right - left);
27+
}
28+
29+
return answer;
30+
}
31+
};
32+
33+
int main() {
34+
Solution soln;
35+
vector nums{0,0,0,0,0,0};
36+
cout << soln.countFairPairs(nums, 0 , 0) << endl;
37+
return 0;
38+
}

0 commit comments

Comments
 (0)