fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int arr[]={1,2,3,4,5};
  6. int maxt=0 ,sum=0 ,k=10;
  7. // for(int i=0;i<5;i++){
  8. // sum=0;
  9. // for(int j=i;j<5;j++){
  10. // sum+=arr[j];
  11. // if(sum<=k){
  12. // maxt=max(maxt,j-1+1);
  13. // }
  14.  
  15. // }
  16. // }
  17. // cout<<maxt<<endl;
  18.  
  19. for(int i=0 ,j=0; j<5;j++){
  20. sum+=arr[j];
  21. while(sum>k){
  22. sum-=arr[i];
  23. i++;
  24. }
  25. maxt=max(maxt,j-i+1);
  26. }
  27.  
  28. cout<<maxt;
  29. return 0;
  30. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
4