Minimum value to be added to the prefix sums at each array indices to make them positive
Last Updated :
22 Apr, 2021
Given an array arr[] consisting of N of integers, the task is to find the minimum positive value S that needs to be added such that the prefix sum at each index of the given array after adding S is always positive.
Examples:
Input: arr[] = {-3, 2, -3, 4, 2}
Output: 5
Explanation:
For S = 5, prefix sums at each array indices are as follows:
At index 1: (5 – 3) = 2
At index 2: (2 + 2) = 4
At index 3: (4 – 3) = 1
At index 4: (1 + 4) = 5
At index 5: (5 + 2) = 7
Since all the prefix sums obtained is greater than 0, the required answer is 5.
Input: arr[] = {1, 2}
Output: 1
Naive Approach: The simplest approach is to iterate over all possible values of S starting from 1, and add S to the prefix sum at each array indices and check if it is greater than 0 or not. Print that value of S for which all the prefix sums are found to be positive.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, follow the steps below:
- Initialize a variable minValue with 0 to store the minimum prefix sum.
- Initialize a variable sum to store the prefix sum at every index.
- Traverse the array arr[] over range [0, N – 1] using the variable i.
- Update the sum and add arr[i] to the sum.
- If sum is less than minValue, set minValue as sum.
- Initialize a variable startValue to store the desired result and set startValue equal to (1 – minValue).
- After the above steps, print the value of startValue as minimum possible value of S.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
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));
}
}
|
Python3
def minStartValue(nums):
minValue = 0
sum = 0
for i in range ( len (nums)):
sum + = nums[i]
minValue = min (minValue, sum )
startValue = 1 - minValue
return startValue
if __name__ = = '__main__' :
nums = [ - 3 , 2 , - 3 , 4 , 2 ]
print (minStartValue(nums))
|
C#
using System;
class GFG{
static int minStartValue( int [] nums)
{
int minValue = 0;
int sum = 0;
foreach ( int n in nums)
{
sum += n;
minValue = Math.Min(minValue, sum);
}
int startValue = 1 - minValue;
return startValue;
}
public static void Main()
{
int [] nums = { -3, 2, -3, 4, 2 };
Console.WriteLine(minStartValue(nums));
}
}
|
Javascript
<script>
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));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...