Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead.

Input: target = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: The subarray [4,3] has the minimal length under the problem constraint.
Input: target = 4, nums = [1,4,4]
Output: 1
Input: target = 11, nums = [1,1,1,1,1,1,1,1]
Output: 1
Constraints:

Contents

In this approach, we are going to apply sliding window technique using two pointers left and right, keep track of minimum window size when the sum of elements in that window is greater than or equal to the target.

Implementation steps:
public class MinimumSizeSubArraySum { static int minSubArrayLen(int target, int[] nums) { int left = 0; int right = 0; int sum =0; int min = nums.length + 1; while(right < nums.length) { sum += nums[right]; while(sum >=target) { min = Math.min(right - left +1 , min); sum-=nums[left]; left++; } right++; } return min == nums.length +1 ? 0 : min; } public static void main(String[] args) { System.out.println(minSubArrayLen(7, new int[]{2,3,1,2,4,3})); System.out.println(minSubArrayLen(11, new int[]{1,1,1,1,1,1,1,1})); } }
Complexity Analysis:

Time complexity: Above code runs in O(n) time, where n is the length of input array nums.
Space complexity: O(1).

Above implementations source code can be found at GitHub link for Java code