Back to photostream

Minimum Value to Get Positive Step by Step Sum Leetcode Solution

Problem Statement

In this problem, we are given a sequence of numbers (may be positive negative or zero). We have to take a positive integer with us and then we will start adding all integers of this array from left to right with it.

We want the minimum positive integer that we should take in the start, so that, at any time our current sum will always remain positive.

Example

nums =

5

Explanation:

 

We can see here that if we choose startValue=5, we get all intermediate sum positive. We can check for startValue=4 also, which is not correct solution.

nums =

1

Explanation:

 

Minimum start value should be positive.

Approach

Suppose, we have array , nums =

Now if we choose initial value as 2, and keep adding elements from left to right, then:

 

In above example, we have chosen initial value as 2. Our sum will not remain positive every time, so we need some larger element.

 

Let the initial val be 5.

 

Now, we can clearly see that if starting value is 5 then, we can surely travel throughout the array keeping our current sum positive always. 5 can be the answer if it is the smallest integer doing so.

Let's think of a situation if we choose val=0 with us at the start.

 

Now, can we say that if we overcome the value of most negative current sum (-4 in current example), then we can clearly pass the array without any problem.

Like, in above example, the most negative value is -4, To overcome it, we have to make it 1 (because, smallest positive integer needed).

 

www.tutorialcup.com/leetcode-solutions/minimum-value-to-g...

6 views
0 faves
0 comments
Uploaded on September 25, 2021
Taken on December 29, 2020