Minimum value to be added to the prefix sums at each array indices to make them positive | Java program for the above approach ; 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
import java . util . * ; class GFG { static int minStartValue ( int [ ] nums ) { int minValue = 0 ; int sum = 0 ; for ( int n : nums ) { sum += n ; minValue = Math . min ( minValue , sum ) ; } int startValue = 1 - minValue ; return startValue ; } public static void main ( String [ ] args ) { int [ ] nums = { - 3 , 2 , - 3 , 4 , 2 } ; System . out . println ( minStartValue ( nums ) ) ; } }