ExamAdda Logo

Minimum Size Subarray Sum

Easy

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

If no such subarray exists, 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, which is greater than or equal to the target.

Its length is 2, which is the minimum possible.

Example 2

Input

8
1 4 4 2 1 1 1 1
4

Output

1

Explanation

The subarray [4] alone satisfies the condition, so the minimum length is 1.

Constraints

1 ≤ n ≤ 10⁵
1 ≤ nums[i] ≤ 10⁴
1 ≤ target ≤ 10⁹

Hints:

Hint 1

Maintain a window using two pointers and keep track of the current window sum.

Hint 2

Expand the window by moving the right pointer. Whenever the window sum becomes greater than or equal to the target, shrink it from the left while updating the minimum 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

Jul 29, 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