fork download
  1. #include <iostream>
  2. using namespace std;
  3. #include <bits/stdc++.h>
  4. typedef long long int ll;
  5. int main() {
  6. // your code goes here
  7. ll n;
  8. cin>>n;
  9. ll arr[n+5]={0};
  10. ll k;
  11. cin>>k;
  12. ll i=1;
  13. while(i<=n)
  14. {
  15. cin>>arr[i];
  16. i++;
  17. }
  18.  
  19. i=1;
  20. ll l,r;
  21. ll b[20005]={0};
  22. while(i<=n)
  23. {
  24. l=arr[i]-k;
  25. r=arr[i]+k;
  26.  
  27. b[l]=b[l]+1;
  28. b[r+1]=b[r+1]-1;
  29. i++;
  30. }
  31.  
  32. ll ans=1;
  33. i=1;
  34. while(i<=2000)
  35. {
  36. b[i]=b[i]+b[i-1];
  37. ans=max(ans,b[i]);
  38. i++;
  39. }
  40.  
  41.  
  42. cout<<ans;
  43. return 0;
  44. }
Success #stdin #stdout 0.01s 5276KB
stdin
3
3
5 8 10
stdout
3