Minimum Value for Positive Step by Step Sum

Easy
6
34.1% Acceptance

In this lab, you will implement a function minStartValue(nums) that calculates the minimal initial positive value for the given array of integers, nums. The goal is to find the minimum positive value of startValue such that the step by step sum of startValue plus elements in nums (from left to right) is never less than 1.

Function signature

/** * @param {number[]} nums * @return {number} */ export function minStartValue(nums) { }

Examples

Example 1

minStartValue([-3, 2, -3, 4, 2])

Output: 5

Explanation: If you choose startValue = 4, in the third iteration your step by step sum is less than 1.

step by step sum startValue = 4 | startValue = 5 | nums (4 -3 ) = 1 | (5 -3 ) = 2 | -3 (1 +2 ) = 3 | (2 +2 ) = 4 | 2 (3 -3 ) = 0 | (4 -3 ) = 1 | -3 (0 +4 ) = 4 | (1 +4 ) = 5 | 4 (4 +2 ) = 6 | (5 +2 ) = 7 | 2

Example 2

minStartValue([1, 2])

Output: 1

Explanation: Minimum start value should be positive.

Example 3

minStartValue([1, -2, -3])

Output: 5

Constraints

  • 1 <= nums.length <= 100
  • -100 <= nums[i] <= 100

In this lab, you will be required to complete the following challenges:

  1. Implement the minStartValue(nums) function.
  2. Export the minStartValue(nums) function.
  3. Test the minStartValue(nums) function using the provided evaluation script.

Please remember to export any functions, variables, or values necessary for your challenges inside the respective challenge block text. Also, import any user code dynamically inside every challenge to avoid lab crashes if the required file(s) are not present.