Minimum value to be added to the prefix sums at each array indices to make them positive | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minStartValue ( vector < int > & nums ) { int minValue = 0 ; int sum = 0 ; for ( auto n : nums ) { sum += n ; minValue = min ( minValue , sum ) ; } int startValue = 1 - minValue ; return startValue ; } int main ( ) { vector < int > nums = { -3 , 2 , -3 , 4 , 2 } ; cout << minStartValue ( nums ) ; return 0 ; }