Skip to content

Commit 3ee9ad3

Browse files
authored
Merge pull request #442 from suzzzal/day07sol01
Day07sol01
2 parents a575a06 + 9f73b85 commit 3ee9ad3

2 files changed

Lines changed: 59 additions & 92 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// submisiion link - https://codeforces.com/problemset/submission/1538/356092904
2+
3+
/*
4+
* APPROACH:
5+
* First, sort all the numbers from smallest to largest so it's easier to find pairs.
6+
* Then, for each number, search quickly (using binary search) for other bigger numbers
7+
* that can be added to it to make a sum between 'l' and 'r'. Count them up!
8+
*/
9+
10+
11+
#include <bits/stdc++.h>
12+
using namespace std;
13+
14+
15+
void solve() {
16+
int n;
17+
long long l, r;
18+
cin >> n >> l >> r;
19+
20+
21+
vector<long long> a(n);
22+
for (int i = 0; i < n; i++) {
23+
cin >> a[i];
24+
}
25+
26+
sort(a.begin(), a.end());
27+
long long count = 0;
28+
29+
for (int i = 0; i < n; i++) {
30+
31+
auto start_ptr = a.begin() + i + 1;
32+
auto end_ptr = a.end();
33+
auto low = lower_bound(start_ptr, end_ptr, l - a[i]);
34+
auto high = upper_bound(start_ptr, end_ptr, r - a[i]);
35+
36+
count += (high - low);
37+
38+
}
39+
40+
cout << count << endl;
41+
42+
}
43+
44+
45+
46+
int main() {
47+
48+
int t;
49+
cin >> t;
50+
while (t--) {
51+
solve();
52+
53+
}
54+
55+
56+
57+
return 0;
58+
59+
}

Problems/Binary-Search/Day-07/sol/suzzzal/day07sol02.cpp

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)