fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. int n,k;
  7. cin>>n>>k;
  8. vector<int> arr(n);
  9. for(int i=0;i<n;i++){
  10. cin>>arr[i];
  11. }
  12. int diff =0;
  13. int i=0,j=0;
  14. int ans = 0;
  15. while(j<n){
  16. diff = abs(arr[j]-arr[i]);
  17. if(diff<=k){
  18. ans=max(ans,j-i+1);
  19. }else if(diff>k){
  20. while(diff>k&&i<=j){
  21. diff = abs(arr[j]-arr[i]);
  22. i++;
  23. }
  24. }
  25.  
  26. j++;
  27. }
  28. cout<<ans;
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0.01s 5268KB
stdin
7 4
1 2 3 6 7 8 10
stdout
3