Minimum Value to Get Positive Step by Step Sum

Easy
490.

Minimum Value to Get Positive Step by Step Sum

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.

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
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number[]} nums
 * @return {number}
 */
var minStartValue = function(nums) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

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

  1. Hint 1

    The running total after k steps is startValue + prefix[k]. Only the prefix sums depend on the array.

  2. Hint 2

    The tightest constraint comes from the smallest prefix sum.

  3. Hint 3

    You need startValue + minPrefix >= 1, so startValue = 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

  1. Sweep the array keeping a running sum acc.
  2. Track lowest, the minimum value acc ever reaches — initialised to 0 so an all-positive array is handled.
  3. 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 lowest to nums[0] or to +infinity breaks the all-positive case, which must answer 1.
  • 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 - lowest

Java

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