ExamAdda Logo

Minimum Size Subarray Sum

Easy

Given an array of positive integers nums and a positive integer target, return the minimal length of a contiguous subarray whose sum is greater than or equal to target.

If there is no such subarray, return 0.

Example 1

Input

6
2 3 1 2 4 3
7

Output

2

Explanation

The subarray [4, 3] has a sum of 7.

Its length is 2, which is the minimum possible.

Example 2

Input

1
1
1

Output

1

Explanation

The single element itself satisfies the target sum.

Constraints

1 ≤ target ≤ 10⁹
1 ≤ nums.length ≤ 10⁵
1 ≤ nums[i] ≤ 10⁴

Hints:

Hint 1

Since all elements are positive, increasing the window always increases the sum, and shrinking the window always decreases it.

Hint 2

Use the sliding window technique. Expand the right pointer until the sum reaches or exceeds the target, then shrink the left pointer while maintaining the condition to find the minimum window length.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 5, 2026

Last updated

Aug 6, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output