File tree Expand file tree Collapse file tree
Problems/Mathematics/Day-08/sol/Naman2251 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments