fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. int n = 5;
  5. vector<int> v = {1,9,2,3,5};
  6. int k = 10;
  7. int cursum=0, res=0,i=0,j=0;
  8. while(j<n) {
  9. cursum+=v[j];
  10. if(cursum==k){ res=max(res,j-i+1); }
  11. while(i<=j && cursum > k) {
  12. cursum-=v[i];
  13. i++;
  14. }
  15. j++;
  16. }
  17. cout<<res<<" ";
  18. return 0;
  19. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
3