Minimum Value to Get Positive Step by Step Sum

Given an array of integers nums, you start with an initial positive value startValue.

In each iteration, you calculate the step-by-step sum of startValue plus elements in nums from left to right.

Return the minimum positive value of startValue such that the step-by-step sum is never less than 1.

Example 1
Inputnums = [-3,2,-3,4,2]
Output5
If you choose startValue = 4, the third step-by-step sum becomes 0, so the minimum valid startValue is 5.
Example 2
Inputnums = [1,2]
Output1
The cumulative sums are already positive when startValue is 1, so the minimum start value should be positive.

Constraints

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

Asked at 9 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