209. Minimum Size Subarray Sum

PHOTO EMBED

Tue Feb 28 2023 18:51:46 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar #c++

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int n=nums.size();
        int l=0,r=0;
        int sum=0, ans=INT_MAX;
        while(r < n)
        {
            sum+=nums[r];
            while(sum >= target) 
            {
                //if(sum==target)
                ans = min(ans, r-l+1);
                sum -= nums[l];
                l++;
            }
            r++;
        }
        if (ans == INT_MAX) return 0;
        return ans;
    }
};
content_copyCOPY

https://leetcode.com/problems/minimum-size-subarray-sum/