This website contains ALL LeetCode **Premium** problems for
**FREE!!**.

All leaked interview problems are collected from Internet.

All leaked interview problems are collected from Internet.

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.

For example, given the array `[2,3,1,2,4,3]`

and `s = 7`

,

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*).

**Credits:**

Special thanks to @Freezen for adding this problem and creating all test cases.

b'

\n## Solution

\n

\n#### Approach #1 Brute force [Time Limit Exceeded]

\n

\n#### Approach #2 A better brute force [Accepted]

\n

\n#### Approach #3 Using Binary search [Accepted]

\n

\n#### Approach #4 Using 2 pointers [Accepted]

\n

\n

'
\n\n

\n\n

**Intuition**

Do as directed in question. Find the sum for all the possible subarrays and update the as and when we get a better subarray that fulfill the requirements ().

\n**Algorithm**

- \n
- Initialize \n \n
- Iterate the array from left to right using :
- \n
- Iterate from the current element to the end of vector using :
- \n
- Find the of elements from index to \n \n
- If sum is greater then :
- \n
- Update \n \n
- Start the next th iteration, since, we got the smallest subarray with starting from the current index. \n

\n

\n

\n - Iterate from the current element to the end of vector using :

**Complexity Analysis**

- \n
- \n
Time complexity: .

\n- \n
- For each element of array, we find all the subarrays starting from that index which is . \n
- Time complexity to find the sum of each subarray is . \n
- Thus, the total time complexity : \n \n

\n - \n
Space complexity: extra space.

\n \n

\n

**Intuition**

In Approach #1, you may notice that the sum is calculated for every surarray in time. But, we could easily find the sum in O(1) time by storing the cumulative sum from the beginning(Memoization). After we have stored the cumulative sum in , we could easily find the sum of any subarray from to .

\n**Algorithm**

- \n
- The algorithm is similar to Approach #1. \n
- The only difference is in the way of finding the sum of subarrays:
- \n
- Create a vector of size of \n \n
- Initialize \n \n
- Iterate over the vector:
- \n
- Update \n \n

\n - Sum of subarray from to is calculated as:\n, , wherein is the sum from ()th element to the th element. \n

\n

**Complexity analysis**

- \n
- \n
Time complexity: .

\n- \n
- Time complexity to find all the subarrays is . \n
- Sum of the subarrays is calculated in time. \n
- Thus, the total time complexity: \n \n

\n - \n
Space complexity: extra space.

\n- \n
- Additional space for vector than in Approach #1. \n

\n

\n

**Intuition**

We could further improve the Approach #2 using the binary search. Notice that we find the subarray with starting with an index in time. But, we could reduce the time to using binary search. Note that in Approach #2, we search for subarray starting with index , until we find that is greater than . So, instead of iterating linearly to find the sum, we could use binary search to find the index that is not lower than in the , which can be done using function in C++ STL or could be implemented manually.

\n**Algorithm**

- \n
- \n
Create vector of size with :\n\n

\n \n - \n
Iterate from to :

\n- \n
- Find the value in required for minimum subarray starting from index to have sum greater than , that is:\n\n \n
- Find the index in such that value at that index is not lower than the value, say \n \n
- If we find the in , then:
- \n
- Size of current subarray is given by:\n \n \n
- Compare with the current subarray size and store minimum in \n \n

\n

\n

**Complexity analysis**

- \n
- Time complexity: .
- \n
- For each element in the vector, find the subarray starting from that index, and having sum greater than using binary search. Hence, the time required is for iteration over the vector and for finding the subarray for each index using binary search. \n
- Therefore, total time complexity = \n \n

\n - Space complexity: . Additional space for vector \n

\n

**Intuition**

Until now, we have kept the starting index of subarray fixed, and found the last position. Instead, we could move the starting index of the current subarray as soon as we know that no better could be done with this index as the starting index. We could keep 2 pointer,one for the start and another for the end of the current subarray, and make optimal moves so as to keep the greater than as well as maintain the lowest size possible.

\n**Algorithm**

- \n
- Initialize pointer to 0 and to 0 \n
- Iterate over the :
- \n
- Add to \n \n
- While is greater than or equal to :
- \n
- Update , where is the size of current subarray \n
- It means that the first index can safely be incremented, since, the minimum subarray starting with this index with has been achieved \n
- Subtract from and increment \n \n

\n

\n

**Complexity analysis**

- \n
- Time complexity: . Single iteration of .
- \n
- Each element can be visited atmost twice, once by the right pointer() and (atmost)once by the pointer. \n

\n - Space complexity: extra space. Only constant space required for , , and . \n

\n

Analysis written by @abhinavbansal0.

\n