Skip to content

Commit 18cb9d6

Browse files
authored
Merge pull request #496 from Naman2251/main2
day8sol1
2 parents 54498e4 + 2227652 commit 18cb9d6

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//#Submission Link: https://codeforces.com/contest/2098/submission/356258492
2+
3+
/*
4+
If n-k is odd, the median is a single specific element in the middle of the subset. So, we calculate median by taking first(n-k)
5+
houses and then last (n-k) houses. All houses betwen them an them are possible. so ans is
6+
(median of last n-k)-(median of first n-k)+1.
7+
Similarly for even case, but in this case we have two medians so we take bigger one for last n-k and smaller one for first n-k.
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
int main() {
14+
long long t;
15+
cin>>t;
16+
while(t--) {
17+
long long n, k;
18+
cin>>n>>k;
19+
vector<long long> a(n+1);
20+
for(int i=1; i<=n; i++) cin>>a[i];
21+
sort(a.begin(), a.end());
22+
if((n-k)%2!=0) cout<<a[(n+k+1)/2]-a[(n-k+1)/2]+1<<endl;
23+
else cout<<a[(n+k)/2+1]-a[(n-k)/2]+1<<endl;
24+
}
25+
}

0 commit comments

Comments
 (0)