Minimum value to be added to the prefix sums at each array indices to make them positive | Function to find minimum startValue for positive prefix sum at each index ; Store the minimum prefix sum ; Stores prefix sum at each index ; Traverse over the array ; Update the prefix sum ; Update the minValue ; Return the positive start value ; Driver Code ; Function Call
function minStartValue ( nums ) { let minValue = 0 ; let sum = 0 ; for ( let n = 0 ; n < nums . length ; n ++ ) { sum += nums [ n ] ; minValue = Math . min ( minValue , sum ) ; } let startValue = 1 - minValue ; return startValue ; } let nums = [ - 3 , 2 , - 3 , 4 , 2 ] ; document . write ( minStartValue ( nums ) ) ;