fork download
  1. #include <iostream>
  2. using namespace std;
  3. #include<bits/stdc++.h>
  4.  
  5. int main() {
  6. // your code goes here
  7. vector<int>arr = {1,2,2,1,2};
  8. int i = 0 ; int j = 0 ; int k = 4;
  9. int n = arr.size(); int sum = 0 ; int count = 0 ;
  10.  
  11. while(i<n&&j<n){
  12.  
  13. sum+=arr[j];
  14. while(sum>k){
  15. sum-=arr[i];
  16. i++;
  17. }
  18. count+=j-i+1;
  19. j++;
  20.  
  21. }
  22. cout<<count ;
  23.  
  24. }
Success #stdin #stdout 0.01s 5308KB
stdin
Standard input is empty
stdout
9