fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5. int t,n,k,x,mx,mn; cin >> t;
  6. while(t--){
  7. cin >> n >> k;
  8. map<int,int> f;
  9. for(int i=0;i<n;i++){
  10. cin >> x;
  11. f[x]++;
  12. }
  13. auto it1 = f.begin();
  14. auto it2 = f.rbegin();
  15. mn = it1->first, mx = it2->first;
  16. mn + k >= mx - k ? cout << mn+k << endl : cout << "-1\n";
  17. }
  18. return 0;
  19. }
Success #stdin #stdout 0.01s 5284KB
stdin
4
5 1
1 1 2 3 1
4 2
6 4 8 5
2 2
1 6
3 5
5 2 5
stdout
2
6
-1
7