fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void solve(){
  5. int n, k; cin >> n >> k;
  6.  
  7. vector<int> a(n);
  8.  
  9. for (int i = 0; i < n; i++){
  10. cin >> a[i];
  11. }
  12.  
  13. sort(a.begin(), a.end());
  14.  
  15. int total = 0;
  16.  
  17. int j = n-2;
  18. int i = n-1;
  19.  
  20. while (j >= 0 && i > 0){
  21. if (i == j) j--;
  22. if (a[j] + k >= a[i] && a[i] > a[j]){
  23. total++;
  24. j--;
  25. }
  26. else{
  27. i--;
  28. }
  29. }
  30. cout << n-total << '\n';
  31. }
  32.  
  33. int main(){
  34. solve();
  35. }
Success #stdin #stdout 0s 5324KB
stdin
7 1
101 53 42 102 101 55 54
stdout
3