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.

30:00

Minimum Value to Get Positive Step by Step Sum
easy
Free written solution
Topics
Companies

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:
Input: {"nums":[-3,2,-3,4,2]}
Output: 5
Constraints:
  • 1≤nums.length≤1001 \leq \text{nums.length} \leq 100

  • −100≤nums[i]≤100-100 \leq \text{nums}[i] \leq 100

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[-3,2,-3,4,2]}

Walk the sums starting from 0. Whatever startValue we pick lifts the whole path by the same amount, so we only need its lowest point.

nums (each step added to the total)

-3
+2
-3
+4
+2

running total from 0

0

total = 0

lowest = 0

startValue = 1 - lowest

Variables
VariableValue
total0
lowest0
DepthFunction Call
Stack empty
0/11