File tree Expand file tree Collapse file tree
Problems/Binary-Search/Day-07/sol/suzzzal Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments