Skip to main content
Back to problems
#1413
Easy Algorithms

Minimum value to get positive step by step sum

Array Prefix Sum
64.6% acceptance
Feb 25, 2026
1671
387
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn min_start_value(nums: Vec<i32>) -> i32 {
    let mut min_prefix = 0;
    let mut prefix = 0;
    for n in nums {
      prefix += n;
      min_prefix = min_prefix.min(prefix);
    }
    (1 - min_prefix).max(1)
  }
}