File tree Expand file tree Collapse file tree
Problems/Binary-Search/Day-07/sol/Apoorv012 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /*
2+ Submission Link: https://codeforces.com/contest/1538/submission/356110702
3+
4+ Instead of finding the pairs to be greater than equal to L, and less than equal to R
5+ I found the pairs that were less than equal to R separately, and less than L separately.
6+ and the answer was the difference of it.
7+
8+ */
9+
10+ #include < bits/stdc++.h>
11+ using namespace std ;
12+
13+ int main () {
14+ int t;
15+ cin >> t;
16+ while (t--) {
17+ long long n, L, R;
18+ cin >> n >> L >> R;
19+ vector<long long > v (n);
20+ for (auto &x: v) cin >> x;
21+ sort (v.begin (), v.end ());
22+ long long lteR = 0 , ltL = 0 ;
23+
24+ long long l = 0 , r = n-1 ;
25+ while (l < r) {
26+ long long s = v[l] + v[r];
27+ // 1 2 3 4 5 ; 3-5
28+ if (s <= R) {
29+ lteR += r-l;
30+ l++;
31+ }
32+ else {
33+ r--;
34+ }
35+ }
36+ l = 0 , r = n-1 ;
37+ while (l < r) {
38+ long long s = v[l] + v[r];
39+ // 1 2 3 4 5 ; 3-5
40+ if (s < L) {
41+ ltL += r-l;
42+ l++;
43+ }
44+ else {
45+ r--;
46+ }
47+ }
48+ cout << (lteR - ltL) << endl;
49+ // cout << lteR << " " << ltL << " " << (lteR - ltL) << endl;
50+ }
51+ }
You can’t perform that action at this time.
0 commit comments