Easy O(n) solution using sliding window


#1

int Solution::solve(vector &A, int B) {
int sum=0;
int ans=0;
int st=0;
int end=0;
while(end<A.size())
{
sum=sum+A[end];
if(sum<B)
{
end++;
ans=ans+(end-st);
}
else
{
while(sum>=B)
{
sum=sum-A[st];
st++;
}
end++;
ans=ans+(end-st);
}
}
return ans;
}