fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5. int t,n,k,x,mx; 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 it = f.begin();
  14. //cout << it->first << endl;
  15. mx = (it->first) + k;
  16. //cout << "1.mx = " << mx << endl;
  17. f[mx]++;
  18. it = f.find(mx);
  19. if(it->second > 1){
  20. cout << it->second << endl;
  21. continue;
  22. }else{
  23. cout << "2.mx = " << mx << endl;
  24. mx--;
  25. while(f.find(mx) == f.end() && mx > 0) mx--;
  26. it = f.find(mx);
  27. f.find(mx) == f.begin() ? cout << "-1\n" : cout << it->second << endl;
  28. }
  29. cout << endl;
  30.  
  31. }
  32. return 0;
  33. }
Success #stdin #stdout 0.01s 5288KB
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
2
2.mx = 3
-1

2.mx = 7
2