Skip to main content

Minimum Positive Sum Subarray - Solution & Explanation

Practice this problem

Problem Statement

You are given an integer array nums and two integers l and r. Your task is to find the minimum sum of a subarray whose size is between l and r (inclusive) and whose sum is greater than 0.

Return the minimum sum of such a subarray. If no such subarray exists, return -1.

A subarray is a contiguous non-empty sequence of elements within an array.

 

Example 1:

Input: nums = [3, -2, 1, 4], l = 2, r = 3

Output: 1

Explanation:

The subarrays of length between l = 2 and r = 3 where the sum is greater than 0 are:

  • [3, -2] with a sum of 1
  • [1, 4] with a sum of 5
  • [3, -2, 1] with a sum of 2
  • [-2, 1, 4] with a sum of 3

Out of these, the subarray [3, -2] has a sum of 1, which is the smallest positive sum. Hence, the answer is 1.

Example 2:

Input: nums = [-2, 2, -3, 1], l = 2, r = 3

Output: -1

Explanation:

There is no subarray of length between l and r that has a sum greater than 0. So, the answer is -1.

Example 3:

Input: nums = [1, 2, 3, 4], l = 2, r = 4

Output: 3

Explanation:

The subarray [1, 2] has a length of 2 and the minimum sum greater than 0. So, the answer is 3.

 

Constraints:

  • 1 <= nums.length <= 100
  • 1 <= l <= r <= nums.length
  • -1000 <= nums[i] <= 1000

Approach Overview

Problem Overview: You are given an integer array and two integers l and r. The task is to find the smallest positive sum of any subarray whose length is between l and r (inclusive). If no such subarray exists, return -1.

Approach 1: Brute Force Subarray Sum (O(n²) time, O(1) space)

The most direct solution is to enumerate every possible subarray and compute its sum. For each starting index, expand the subarray one element at a time and keep a running sum. Whenever the current length falls within the range [l, r], check if the sum is positive and update the minimum if needed. This approach relies on basic array iteration and works because every candidate subarray is evaluated explicitly. Time complexity is O(n²) in the worst case since each start index may expand up to n elements, while space usage stays O(1).

Approach 2: Sliding Window by Length (O(n * (r-l+1)) time, O(1) space)

A more structured solution uses the sliding window technique. Instead of recomputing sums repeatedly, iterate over every valid window size from l to r. For each size, compute the first window sum, then slide the window forward by subtracting the outgoing element and adding the incoming one. This keeps each window update O(1). Every time the window moves, check whether the sum is positive and update the global minimum. Internally this behaves similarly to a fixed-length window using incremental updates rather than recomputation.

This technique avoids repeatedly summing the same elements and makes the logic cleaner. The algorithm effectively processes all subarrays whose sizes fall in the allowed range while maintaining the running sum dynamically. The total work becomes O(n * (r-l+1)), which is significantly better than recomputing sums for each candidate window.

Another way to think about the computation is through prefix sums, where the sum of any subarray can be obtained using prefix[j] - prefix[i]. Sliding windows are essentially an optimized version of this idea when window sizes are fixed.

Recommended for interviews: Start by describing the brute force enumeration because it shows you understand the search space of subarrays. Then move to the sliding window optimization that maintains a running sum for each window size. Interviewers typically expect the optimized window-based approach since it avoids repeated summation and demonstrates familiarity with common array optimization patterns.

Approach 1: Brute Force Subarray Sum

This approach involves calculating the sum of all possible subarrays within the given length range and finding the minimum sum greater than zero.

We iterate through all possible subarray starting and ending indices, calculating the sum for each subarray. If a sum is greater than zero and less than the current minimum, it's the new minimum.

This C code iterates through each possible starting index of a subarray and calculates the sums of incrementing subarrays up to length r. Whenever a subarray's length is within [l, r] and the sum is positive and less than the current minimum sum, it updates the minimum.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n^2) where n is the length of the array.
Space Complexity: O(1)

Try this approach in the editor →

Approach 2: Sliding Window Approach

A sliding window approach allows us to build the subarray sum incrementally, avoiding recalculating the sum by constantly adding the next element and removing the old one. This approach is optimized by restricting the window size to the given range.

For each window ending point, we compute the sum and check if expanding the window will yield a valid subarray length while keeping track of the minimum positive sum.

This C code uses a sliding window that expands as new elements are added, and contracts by moving the start pointer for maintaining a valid subarray length. It ensures all valid subarray sums are checked.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n) where n is the length of the array.
Space Complexity: O(1)

Try this approach in the editor →

Approach 3: Enumeration

We can enumerate the left endpoint i of the subarray, then enumerate the right endpoint j from i to n within the interval [i, n). We calculate the sum s of the interval [i, j]. If s is greater than 0 and the interval length is between [l, r], we update the answer.

Finally, if the answer is still the initial value, it means no subarray meets the conditions, so we return -1. Otherwise, we return the answer.

The time complexity is O(n^2), where n is the length of the array nums. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Brute Force Subarray Sum

Time Complexity: O(n^2) where n is the length of the array.
Space Complexity: O(1)

Sliding Window Approach

Time Complexity: O(n) where n is the length of the array.
Space Complexity: O(1)

Enumeration

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Subarray EnumerationO(n²)O(1)Useful for understanding the problem and validating logic on small inputs.
Sliding Window by LengthO(n * (r-l+1))O(1)Preferred approach when window sizes are bounded; avoids recomputing sums.

Video Solution

Minimum Positive Sum Subarray | Leetcode 3364Code Crafters1,835 views views

Watch 7 more video solutions →

Frequently Asked Questions

Is Minimum Positive Sum Subarray easy or hard?
Minimum Positive Sum Subarray is categorized as an Easy problem. The main challenge is correctly iterating through subarrays within the allowed length range and keeping track of the smallest positive sum.
Minimum Positive Sum Subarray Python/Java solution
Python, Java, C++, and JavaScript implementations typically follow the same idea: iterate over valid window sizes and maintain the current sum while sliding the window. Each step updates the minimum positive sum if the current window sum is greater than zero.
How to solve Minimum Positive Sum Subarray in O(n)?
Pure O(n) is difficult when the subarray length must be between l and r because multiple window sizes must be evaluated. The practical optimization is to use a sliding window for each valid length, giving O(n * (r-l+1)) time. Prefix sums can also help compute subarray sums quickly.
Is Minimum Positive Sum Subarray asked at Google/Amazon/Meta?
Problems based on subarray sums, sliding window techniques, and prefix sums appear frequently in interviews at companies like Amazon, Google, and Meta. Variants of constrained subarray problems are especially common in coding rounds.
What is the best approach for Minimum Positive Sum Subarray ?
The sliding window approach is typically the best practical solution. Iterate through every window size between l and r and maintain the window sum while sliding across the array. This avoids recomputing sums for each subarray and runs in O(n * (r-l+1)) time with O(1) extra space.
What data structure is used in Minimum Positive Sum Subarray ?
The solution mainly uses arrays and running sums. Sliding window techniques maintain a dynamic sum while moving across the array, and prefix sums can be used to compute subarray sums efficiently.
What is the time complexity of Minimum Positive Sum Subarray ?
The brute force solution runs in O(n²) time because it checks many possible subarrays. The optimized sliding window method processes each window in constant time and runs in O(n * (r-l+1)). Both approaches use O(1) additional space.

Ready to solve this problem?

Practice Minimum Positive Sum Subarray with our built-in code editor and test cases.

Practice on FleetCode