# Blogs Hub

### by Sumit Chourasia | Nov 18, 2020 | Category :coding | Tags : algorithmarraydata-structureleetcodemedium #### Minimum Size Subarray Sum - Array - Medium - LeetCode

Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.

Example:

Input: s = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: the subarray [4,3] has the minimal length under the problem constraint.
If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).

``````public class Solution {
public int MinSubArrayLen(int s, int[] nums) {
if (nums.Length == 0)
{
return 0;
}

int start = 0;
int end = 0;
int sum = nums;
int min = int.MaxValue;
while (true)
{
if (sum >= s)
{
if (end - start+1 < min)
{
min = end - start+1;
}
sum -= nums[start];
start++;
}
else
{
end++;
if(end > nums.Length - 1)
{
break;
}
sum += nums[end];
}
}

return min == int.MaxValue ? 0 : min;
}
}``````

Time Complexity: O(n)

Space Complexity: O(1)