Minimum Value to Get Positive Step by Step Sum — Solution
Minimum Value to Get Positive Step by Step Sum solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript.
Minimum Value to Get Positive Step by Step Sum is an easy coding problem on arrays and prefix sum, asked in Amazon, Microsoft and Yelp interviews.
- Difficulty: Easy
- Topics: Arrays, Prefix Sum
- Asked at: Amazon, Microsoft, Yelp
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
You start with some positive integer startValue and walk left to right through nums, adding each element to a running total.
Return the minimum positive startValue such that the running total is never less than 1 at any point.
Example 1
Input: nums = [-3,2,-3,4,2]
Output: 5
Explanation: Starting at 5 the totals are 2, 4, 1, 5, 7 — never below 1. Starting at 4 gives 1, 3, 0 — too low.
Example 2
Input: nums = [1,2]
Output: 1
Explanation: The running total never dips, so the smallest allowed start works.
Example 3
Input: nums = [1,-2,-3]
Output: 5
Constraints
1 <= nums.length <= 100-100 <= nums[i] <= 100
Hints for Minimum Value to Get Positive Step by Step Sum
Hint 1
The running total after
ksteps isstartValue + prefix[k]. Only the prefix sums depend on the array.Hint 2
The tightest constraint comes from the smallest prefix sum.
Hint 3
You need
startValue + minPrefix >= 1, sostartValue = 1 - minPrefix— clamped to at least 1.
How to solve Minimum Value to Get Positive Step by Step Sum
Adding a constant start value shifts every running total by the same amount, so the only prefix that matters is the lowest one.
Approach
- Sweep the array keeping a running sum
acc. - Track
lowest, the minimum valueaccever reaches — initialised to0so an all-positive array is handled. - Return
1 - lowest.
Why it works
The constraint startValue + prefix[k] >= 1 must hold for every k; the binding one is the minimum prefix. Seeding lowest at 0 encodes the separate rule that startValue itself must be at least 1.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
- Initialising
lowesttonums[0]or to+infinitybreaks the all-positive case, which must answer1. - Binary searching the start value works but is unnecessary — the closed form is exact.
Reference solution
Python
from typing import List
def minStartValue(nums: List[int]) -> int:
acc = 0
lowest = 0
for x in nums:
acc += x
if acc < lowest:
lowest = acc
return 1 - lowestJava
public static int minStartValue(int[] nums) {
int acc = 0, lowest = 0;
for (int x : nums) {
acc += x;
if (acc < lowest) lowest = acc;
}
return 1 - lowest;
}C++
int minStartValue(vector<int>& nums) {
int acc = 0, lowest = 0;
for (int x : nums) {
acc += x;
if (acc < lowest) lowest = acc;
}
return 1 - lowest;
}JavaScript
var minStartValue = function(nums) {
let acc = 0, lowest = 0;
for (let i = 0; i < nums.length; i++) {
acc += nums[i];
if (acc < lowest) lowest = acc;
}
return 1 - lowest;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays · Prefix Sum