fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4.  
  5. int main() {
  6. ios::sync_with_stdio(false);
  7. cin.tie(nullptr);
  8.  
  9. int t;
  10. cin >> t;
  11. while (t--) {
  12. int n;
  13. ll k;
  14. cin >> n >> k;
  15.  
  16. vector<ll> b(n);
  17. for (int i = 0; i < n; i++) {
  18. cin >> b[i];
  19. }
  20.  
  21. // Sort bar‐positions
  22. sort(b.begin(), b.end());
  23.  
  24. // delta = floor(k/2)
  25. ll delta = k / 2;
  26.  
  27. // If delta > n-1, no valid x
  28. if (delta > n - 1) {
  29. cout << 0 << "\n";
  30. continue;
  31. }
  32.  
  33. // Left endpoint = b[delta], right endpoint = b[n-1-delta]
  34. ll L = b[delta];
  35. ll R = b[n - 1 - delta];
  36. // The interval of valid x is [L..R], inclusive.
  37. ll ans = max(0LL, R - L + 1);
  38. cout << ans << "\n";
  39. }
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 5320KB
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
4
2
4
9