[InterviewBit] Counting Subarrays!

Counting Subarrays!

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
int Solution::solve(vector<int> &A, int B) {
int res = 0, sum = 0, l = 0;
for(int i = 0; i < A.size(); i++) {
sum += A[i];
while(l < i and sum >= B) sum -= A[l++];
if(sum < B) {
res += i - l + 1;
}
}
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/11/08/PS/Codeforces/counting-subarrays/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.