Easy sliding window O(n) solution


#1
public class Solution {
    public int solve(ArrayList<Integer> arr, int target) {
        int n = arr.size();
        int ws = 0,sum = 0, i = 0,ans=0;
        while(ws < n && i < n) {
            if(sum+arr.get(i) < target) {
                sum+=arr.get(i);
                ans+=i-ws+1;
                i++;
            } else{
                sum-=arr.get(ws);
                ws++;
            }
        }
        return ans;
    }
}