fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5.  
  6. signed main() {
  7. int t;
  8. cin >> t;
  9. while (t--) {
  10. int n, k;
  11. cin >> n >> k;
  12. vector<int> a(n);
  13. for (int i = 0; i < n; i++) cin >> a[i];
  14. if (k >= 3) {
  15. cout << 0 << endl;
  16. continue;
  17. }
  18. sort(begin(a), end(a));
  19. int d = a[0];
  20. for (int i = 0; i < n - 1; i++) d = min(d, a[i + 1] - a[i]);
  21. if (k == 1) {
  22. cout << d << endl;
  23. continue;
  24. }
  25. for (int i = 0; i < n; i++) for (int j = 0; j < i; j++) {
  26. int v = a[i] - a[j];
  27. int p = lower_bound(begin(a), end(a), v) - begin(a);
  28. if (p < n) d = min(d, a[p] - v);
  29. if (p > 0) d = min(d, v - a[p - 1]);
  30. }
  31. cout << d << endl;
  32. }
  33. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
0