File tree Expand file tree Collapse file tree
Problems/Mathematics/Day-08/Aiyaan_Mahajan Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /*
2+ DAY 8
3+ Q1 B. Sasha and the Apartment Purchase
4+
5+ */
6+ /*
7+ Sort the bar positions. Since at most k bars can close, exactly n−k bars will remain.
8+ For this many bars, the optimal apartment must lie between the symmetric medians of the remaining bars.
9+ The answer is simply the size of that interval: v[right] - v[left] + 1.
10+
11+ */
12+
13+ #include < bits/stdc++.h>
14+ using namespace std ;
15+
16+ int main () {
17+
18+ int t;
19+ cin >> t;
20+ while (t--) {
21+ int n, k;
22+ cin >> n >> k;
23+
24+ vector<long long > v (n);
25+ for (int i = 0 ; i < n; i++) cin >> v[i];
26+
27+ sort (v.begin (), v.end ());
28+
29+ int m = n - k;
30+ int left = (m - 1 ) / 2 ;
31+ int right = n - 1 - left;
32+
33+ cout << v[right] - v[left] + 1 << " \n " ;
34+ }
35+ return 0 ;
36+ }
37+
38+
39+ // Tc =O(nlogn) SC = O(n)
40+ /*
41+ My submission : https://codeforces.com/contest/2098/submission/356286079
42+ */
You can’t perform that action at this time.
0 commit comments