Skip to content

Commit 99faf4a

Browse files
authored
Merge pull request #502 from Aiyaan-Mahajan/day-1-q8
q1 day8
2 parents f7ff2bf + ade7e0f commit 99faf4a

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

  • Problems/Mathematics/Day-08/Aiyaan_Mahajan
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
*/

0 commit comments

Comments
 (0)