fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main(){
  6.  
  7. cin.tie(0);
  8.  
  9. ios_base::sync_with_stdio(0);
  10.  
  11. //freopen("diamond.in", "r", stdin);
  12.  
  13. //freopen("diamond.out", "w", stdout);
  14.  
  15. int n, k, maxx=0;
  16.  
  17. cin>>n>>k;
  18.  
  19. vector<int> diamond(n);
  20.  
  21. for(int i=0; i<n; i++){
  22.  
  23. cin>>diamond[i];
  24.  
  25.  
  26.  
  27. }
  28.  
  29. //instead of remove diamonds and try everying
  30.  
  31. sort(diamond.begin(), diamond.end());
  32.  
  33. //sort, start from smallest diamond in group
  34.  
  35. for(int i=0; i<n; i++){
  36. int count =0;
  37. for(int j=1;j<n; j++){
  38. if(diamond[j]-diamond[i]<=k) count++;
  39. else break;
  40. }
  41. maxx=max(maxx,count);
  42.  
  43.  
  44. }
  45. cout<<maxx;
  46. }
  47.  
  48.  
  49.  
  50.  
  51.  
Success #stdin #stdout 0.01s 5288KB
stdin
5 3
1
6
4
3
1
stdout
4