Length of smallest subarray with a sum greater than or equal to S
By: Golibrary Co
Tags: Sliding, Smallest, Array

Problem Statement

Given an array of positive numbers and a positive number ‘S’, find the length of the smallest contiguous subarray whose sum is greater than or equal to ‘S’. Return 0, if no such subarray exists.

Example 1:

Input: , S=7

Output: 2

Explanation: The smallest subarray with a sum great than or equal to ‘7’ is .

Example 2:

Input: , S=7

Output: 1

Explanation: The smallest subarray with a sum greater than or equal to ‘7’ is .





Example 3:



Input: , S=8

Output: 3

Explanation: Smallest subarrays with a sum greater than or equal to ‘8’ are or .



One simple solution is the brute force solution as seen in the previous blog

but it’s not effective, so this problem is another example of sliding window pattern and can be solved using the same approach as before.

Efficient solution:

This problem follows the Sliding Window pattern and we can use a similar strategy as discussed in Maximum Sum Subarray of Size K. There is one difference though: in this problem, the size of the sliding window is not fixed. Here is how we will solve this problem:

First, we will add-up elements from the beginning of the array until their sum becomes greater than or equal to ‘S’.



These elements will constitute our sliding window. We are asked to find the smallest such window having a sum greater than or equal to ‘S’. We will remember the length of this window as the smallest window so far.



After this, we will keep adding one element in the sliding window (i.e. slide the window ahead), in a stepwise fashion.



In each step, we will also try to shrink the window from the beginning. We will shrink the window until the window’s sum is smaller than ‘S’ again. This is needed as we intend to find the smallest window. This shrinking will also happen in multiple steps; in each step we will do two things:



Check if the current window length is the smallest so far, and if so, remember its length.

Subtract the first element of the window from the running sum to shrink the sliding window.

Here is the visual representation of this algorithm for the Example-1

minsum-subarray



Javascript code below:-



const smallest_subarray_with_given_sum = function(s, arr) {

// TODO: Write your code here

var windowStart = 0;

var minLength = Number.MAX_SAFE_INTEGER;

var windowSum = 0;

for (var windowEnd = 0; windowEnd < arr.length; windowEnd++) {

windowSum += arr;

while (windowSum >= s) {

minLength = Math.min(minLength, windowEnd — windowStart + 1);

windowSum -= arr;

windowStart++;

}

}

if (minLength === Number.MAX_SAFE_INTEGER)

return 0;

return minLength;

};

document.write(`Smallest subarray length: ${smallest_subarray_with_given_sum(7, )}

`);

document.write(`Smallest subarray length: ${smallest_subarray_with_given_sum(7, )}

`);

document.write(`Smallest subarray length: ${smallest_subarray_with_given_sum(8, )}

`);



Time Complexity

The time complexity of the above algorithm will be O(N)O(N). The outer for loop runs for all elements and the inner while loop processes each element only once, therefore the time complexity of the algorithm will be O(N+N) which is asymptotically equivalent to O(N).

Space Complexity

The algorithm runs in constant space O(1).

READ MORE:- https://www.golibrary.co/length-smallest-subarray-with-a-given-sum/