fork download
  1. #include <iostream>
  2. #include<vector>
  3. using namespace std;
  4.  
  5. int main() {
  6. vector<int> nums={2,1,1,5,8};
  7. int k=4;
  8. int count=0;
  9. int sum=0;
  10. int low=0;
  11. int high=0;
  12.  
  13. for(high=0;high<nums.size();high++) {
  14. sum+=nums[high];
  15. while(sum>k && low<=high) {
  16. sum-=nums[low];
  17. low++;
  18. }
  19. count+=high-low+1;
  20. }
  21. cout<<count<<endl;
  22. return 0;
  23. }
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
6