fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void solve() {
  5. int n, k;
  6. cin >> n >> k;
  7. vector<int> a(n);
  8. for (int& x : a) cin >> x;
  9. sort(a.begin(), a.end());
  10.  
  11. int l = (n - k - 1) / 2; // floor((n - k - 1)/2)
  12. int r = (n + k) / 2; // floor((n + k)/2)
  13.  
  14. cout << a[r] - a[l] + 1 << '\n'; // number of possible medians
  15. }
  16.  
  17. int main() {
  18. ios::sync_with_stdio(false);
  19. cin.tie(nullptr);
  20. int t; cin >> t;
  21. while (t--) solve();
  22. }
  23.  
Success #stdin #stdout 0.01s 5288KB
stdin
4
4 0
1 2 3 4
5 2
7 6 6 7 1
3 1
6 7 9
6 2
5 1 9 10 13 2
stdout
2
2
4
9