> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/two-pointer/209.-minimum-size-subarray-sum.md).

# 209. Minimum Size Subarray Sum

## two pointers - fix start, move end&#x20;

```java
class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int n = nums.length;
        int sum = 0;
        int res = Integer.MAX_VALUE;
        
        int end = 0;
        for (int start = 0; start < n; start++) {
            while (end < n && sum < target) {
                sum += nums[end];
                end++;
            } // when out while end is in next position
            if (sum >= target) {
                res = Math.min(res, end - start); // so it's end - start
            }
            sum -= nums[start];
        }
        return res == Integer.MAX_VALUE ? 0 : res;
    }
}


// ask minimal length of a contiguous subarray

// sum is greater than or equal to target(positive integer)

// no result: 0
```

## two pointers - fix end, move start (my sliding win style)&#x20;

T: O(n)

S: O(1)

```java
class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int sum = 0;
        int start = 0;
        int min = Integer.MAX_VALUE;
        for (int end = 0; end < nums.length; end++) {
            sum += nums[end];
            while (sum >= target) {
                min = Math.min(min, end - start + 1);
                sum -= nums[start];
                start++;
            }
        }
        return min == Integer.MAX_VALUE ? 0 : min;
    }
}ja
```
