Minimum Size Subarray Sum Problem
Minimum Size Subarray Sum Problem — ExecCode Medium DSA Practice
Solve the Minimum Size Subarray Sum problem on ExecCode. Free online medium DSA practice in Sliding Window. Write and run code in Java, C++, Python — no signup required to run.
Problem description
Given an array of positive integers nums and a positive target, return the minimum length of a contiguous subarray whose sum is at least target. If no such subarray exists, return 0. Since all values are positive, expanding right increases the running sum and shrinking left decreases it. Use that monotonic behavior to expand until the window becomes valid, then shrink as much as possible while recording the smallest valid length.
Examples
Input target = 7, nums = [2, 3, 1, 2, 4, 3, 1, 4]; Output 2. Input target = 11, nums = [1, 2, 3, 4, 5]; Output 3. Input target = 100, nums = [1, 2, 3]; Output 0
Constraints
1 ≤ target ≤ 10⁹ 1 ≤ nums.length ≤ 10⁵ 1 ≤ nums[i] ≤ 10⁴
Practice Minimum Size Subarray Sum free on ExecCode. Browse DSA problems, topic map, and placement guides.