Solution easy with o(n)


#1

int Solution::solve(vector &a, int k) {

int count=0;
int i=0;
int j=0;
int n=a.size();
int sum=0;

while(j<n)
{
    sum+=a[j];

    while(sum>=k)
    {
       sum-=a[i];
            i++;
    }
        
    count+=(j-i+1);
    
      j++;
    
}

return count;

}


#2

can you explain count+=(j-i+1); in detail?


#3

Suppose array 2 3 4 5, B=10
if i is at 0, j is at 3
then (at j=3) we do ans+=(j-i+1) means that we are taking all subarrays considering the jth element.
Here, j-i+1 = 4
So for taking 5, we are including [4,5], [3,4,5], [2,3,4,5], and [5]. We are adding these to our previous ans.