fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. int t;
  7. cin>>t;
  8. vector<int> arr(200005);
  9. while(t--){
  10. int n,data,k;
  11. cin>>n>>k;
  12. bool all = true;
  13. for(int i=0;i<n;i++){
  14. cin>>arr[i];
  15. }
  16. int max = 1;
  17. int count = 1;
  18. sort(arr.begin(),arr.begin()+n);
  19. // for(int i=0;i<n;i++){
  20. // cout<<arr[i]<<" ";
  21. // }
  22. // cout<<"\n";
  23. for(int i=1;i<n;i++){
  24. //count++;
  25. if((arr[i]-arr[i-1])>k){
  26. //cout<<max<<' '<<count<<"\n";
  27. max = max>count ? max:count;
  28. count = 1;
  29. }else count++;
  30. }
  31. max = max>count ? max:count;
  32. //cout<<max<<' '<<count<<"\n";
  33. // if(count == (n-1)) cout<<0<<"\n";
  34. // else cout<<n-max<<"\n";
  35. cout<<n-max<<'\n';
  36. }
  37. return 0;
  38. }
Success #stdin #stdout 0.01s 5296KB
stdin
8
2 1
7 9
5 1
1 2 4 5 6
1 2
10
8 3
17 3 1 20 12 5 17 12
4 2
2 4 6 8
5 3
2 3 19 10 8
3 4
1 10 5
8 1
8 3 1 4 5 10 7 3
stdout
1
2
0
5
0
3
1
4