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: 10
💡 Note: Starting with 10: 10+(-10)=0, but we need >= 1, so try 11: 11+(-10)=1, then 1+1=2,3,4,5. Need startValue=10+1=11. Wait, let me recalculate: need 10 to make 10+(-10)=0, but need >=1, so 11 makes 11+(-10)=1. Actually, answer is 10 because 10-10=0, but we need >=1, so answer is 10.

Constraints

  • 1 ≤ nums.length ≤ 100
  • -100 ≤ nums[i] ≤ 100

Visualization

Tap to expand
Minimum Start Value Problem: nums = [-3,2,-3,4,2]Input Array:-32-342Prefix Sums:-3-1-402Minimum prefix sum = -4Required startValue = max(1, 1 - (-4)) = 5This ensures: 5+(-3)=2, 2+2=4, 4+(-3)=1, 1+4=5, 5+2=7All running sums >= 1 ✓
Understanding the Visualization
1
Input Array
Array of integers that will be added step by step
2
Calculate Running Sums
Track prefix sums to find minimum value
3
Determine Start Value
Ensure all sums stay >= 1
Key Takeaway
🎯 Key Insight: Find the minimum prefix sum to determine the buffer needed above 1
Asked in
Amazon 25 Microsoft 18 Google 12
23.4K Views
Medium Frequency
~15 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen