Find the smallest positive start value that keeps every running sum at least 1

Quick Overview

Given an integer array, find the smallest positive starting value so that a running total built from left to right never drops below 1. It tests reasoning about prefix sums and handling the edge case where the array never pulls the total down.

Find the smallest positive start value that keeps every running sum at least 1

Company: JioStar

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an array of integers `nums`, you start with an initial positive value `start_value`. In each iteration, you calculate the step by step sum of `start_value` plus the elements of `nums`, from left to right. Return the minimum positive value of `start_value` such that the step by step sum is never less than 1. ### Function Signature ```python def min_start_value(nums: list[int]) -> int: ``` ### Rules - The step by step sums are `start_value + nums[0]`, then `start_value + nums[0] + nums[1]`, and so on through the last element. Every one of them must be at least 1. - `start_value` itself must be at least 1, even when a smaller start would keep every sum at least 1. ### Constraints - `1 <= len(nums) <= 100` - `-100 <= nums[i] <= 100` ### Examples ```text Input: nums = [-3, 2, -3, 4, 2] Output: 5 ``` With a start of 5 the step by step sums are 2, 4, 1, 5, 7, all at least 1. With a start of 4 they would be 1, 3, 0, ..., and 0 is less than 1. ```text Input: nums = [1, 2] Output: 1 ``` The sums only grow, so any positive start works, and the smallest positive value is 1. ```text Input: nums = [1, -2, -3] Output: 5 ``` A start of 5 gives the sums 6, 4, 1. A start of 4 gives 5, 3, 0, which fails at the last step.

Overview: Given an integer array, find the smallest positive starting value so that a running total built from left to right never drops below 1. It tests reasoning about prefix sums and handling the edge case where the array never pulls the total down.

JioStar logo
JioStar
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an array of integers nums, you start with an initial positive value start_value.

In each iteration, you calculate the step by step sum of start_value plus the elements of nums, from left to right.

Return the minimum positive value of start_value such that the step by step sum is never less than 1.

Function Signature

def min_start_value(nums: list[int]) -> int:

Rules

  • The step by step sums are start_value + nums[0] , then start_value + nums[0] + nums[1] , and so on through the last element. Every one of them must be at least 1.
  • start_value itself must be at least 1, even when a smaller start would keep every sum at least 1.

Constraints

  • 1 <= len(nums) <= 100
  • -100 <= nums[i] <= 100

Examples

Input: nums = [-3, 2, -3, 4, 2]
Output: 5

With a start of 5 the step by step sums are 2, 4, 1, 5, 7, all at least 1. With a start of 4 they would be 1, 3, 0, ..., and 0 is less than 1.

Input: nums = [1, 2]
Output: 1

The sums only grow, so any positive start works, and the smallest positive value is 1.

Input: nums = [1, -2, -3]
Output: 5

A start of 5 gives the sums 6, 4, 1. A start of 4 gives 5, 3, 0, which fails at the last step.