Minimum Value to Get Positive Step by Step Sum - Problem
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.
Input & Output
Example 1 — Negative Values
$
Input:
nums = [-3,2,-3,4,2]
›
Output:
5
💡 Note:
Starting with 5: 5+(-3)=2, 2+2=4, 4+(-3)=1, 1+4=5, 5+2=7. All values stay >= 1.
Example 2 — All Positive
$
Input:
nums = [1,2]
›
Output:
1
💡 Note:
Starting with 1: 1+1=2, 2+2=4. All values stay >= 1, so minimum start value is 1.
Example 3 — Large Negative Start
$
Input:
nums = [-10,1,1,1,1]
›
Output:
11
💡 Note:
Starting with 11: 11+(-10)=1, 1+1=2, 2+1=3, 3+1=4, 4+1=5. All values stay >= 1. With startValue=10: 10+(-10)=0, which violates the constraint.
Constraints
- 1 ≤ nums.length ≤ 100
- -100 ≤ nums[i] ≤ 100
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code