Minimum Positive Sum Subarray

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
Inputnums = [3,-2,1,4], l = 2, r = 3
Output1
Out of the valid subarrays with length between 2 and 3 and positive sum, [3, -2] has the smallest positive sum of 1.
Example 2
Inputnums = [-2,2,-3,1], l = 2, r = 3
Output-1
There is no subarray of length between l and r that has a sum greater than 0, so the answer is -1.

Constraints

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

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate