Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Minimum Size Subarray Sum

Medium
sliding-windowarraybinary-search

Given an array of positive integers nums and a positive integer target, return the minimal length of a contiguous subarray whose sum is ≥ target. If there is no such subarray, return 0.

Input

  • Line 1: space-separated positive integers nums
  • Line 2: integer target

Output: the minimal length, or 0.

Example 1
Input:
2 3 1 2 4 3
7
Output:
2

[4,3] has sum 7 with length 2.

Example 2
Input:
1 1 1 1
11
Output:
0

Constraints

  • 1 ≤ nums.length ≤ 10^5
  • 1 ≤ nums[i] ≤ 10^4
  • 1 ≤ target ≤ 10^9
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.