Maximum array sum with prefix and suffix multiplications with -1 allowed
Last Updated :
25 Jul, 2022
Given N elements (both positive and negative). Find the maximum sum, provided that the first operation is to take some prefix of the sequence and multiply all numbers in this prefix by -1. The second operation is to take some suffix and multiply all numbers in it by -1. The chosen prefix and suffix may intersect.
What is the maximum total sum of the sequence that can be obtained by applying the described operations?
Examples:
Input : -1 -2 -3
Output : 6
Explanation: Multiply prefix {-1, -2} with -1.
Multiply suffix {-3} with -1. We get total
sum as 1 + 2 + 3 = 6
Input : -1 10 -5 10 -2
Output : 18
Explanation: Multiply -1 with prefix {-1} and
multiply -1 with suffix {-2}. Elements after
multiplying {1, 10, -5, 10, 2} and sum is
1 + 10 -5 + 10 + 2 = 18.
Input: -4 2 0 5 0
Output: 11
Explanation: Multiply {-4} with -1. Do not
multiply anything in the suffix, so we get
{4, 2, 0, 5, 0} to get sum as 11.
If desired prefix and suffix intersect, then their common part is remaining with the initial sign, and therefore, this case is equivalent to the case when we take the same suffix and prefix, but without their common part.
We traverse from left to right and see if sum or -the sum is more at any step by multiplying -1 to it, and store the maximum of pre_sum and -pre_sum at any index, and continue this process for all elements.
Then we traverse from end to start, and check whose sum is more either the (prefix_sum at that index + negative sum) or the previous maximum that we obtained, if we find at any index the negative sum + prefix sum at that index appears to be more at any step, then we replace the ans to sum*(-1) + pre_sum.
Implementation:
C++
#include <iostream>
using namespace std;
int maximize( int a[], int n)
{
int presum[n];
int sum = 0;
int max_sum = 0;
for ( int i = 0; i<n ; i++)
{
presum[i] = max_sum ;
max_sum += a[i];
sum += a[i];
max_sum = max(max_sum, -sum);
}
int ans = max(sum, max_sum);
int g = 0;
for ( int i = n-1; i >= 0; --i)
{
g -= a[i];
ans = max(ans, g + presum[i]);
}
return ans;
}
int main() {
int a[] = {-4, 2, 0, 5, 0};
int n = sizeof (a)/ sizeof (a[0]);
cout << maximize(a, n);
return 0;
}
|
Java
import java.math.*;
class GFG {
static int maximize( int a[], int n)
{
int presum[] = new int [n];
int sum = 0 ;
int max_sum = 0 ;
for ( int i = 0 ; i<n ; i++)
{
presum[i] = max_sum ;
max_sum += a[i];
sum += a[i];
max_sum = Math.max(max_sum, -sum);
}
int ans = Math.max(sum, max_sum);
int g = 0 ;
for ( int i = n- 1 ; i >= 0 ; --i)
{
g -= a[i];
ans = Math.max(ans, g + presum[i]);
}
return ans;
}
public static void main(String args[]) {
int a[] = {- 4 , 2 , 0 , 5 , 0 };
int n = a.length;
System.out.println(maximize(a, n));
}
}
|
Python3
def maximize(a,n) :
presum = [ 0 ] * n
sm = 0
max_sum = 0
for i in range ( 0 ,n) :
presum[i] = max_sum
max_sum = max_sum + a[i]
sm = sm + a[i]
max_sum = max (max_sum, - sm)
ans = max (sm, max_sum)
g = 0
for i in range (n - 1 , - 1 , - 1 ) :
g = g - a[i]
ans = max (ans, g + presum[i])
return ans
a = [ - 4 , 2 , 0 , 5 , 0 ]
n = len (a)
print (maximize(a, n))
|
C#
using System;
class GFG
{
static int maximize( int []a, int n)
{
int []presum = new int [n];
int sum = 0;
int max_sum = 0;
for ( int i = 0; i < n ; i++)
{
presum[i] = max_sum ;
max_sum += a[i];
sum += a[i];
max_sum = Math.Max(max_sum,
-sum);
}
int ans = Math.Max(sum, max_sum);
int g = 0;
for ( int i = n - 1; i >= 0; --i)
{
g -= a[i];
ans = Math.Max(ans, g + presum[i]);
}
return ans;
}
public static void Main()
{
int []a = {-4, 2, 0, 5, 0};
int n = a.Length;
Console.WriteLine(maximize(a, n));
}
}
|
PHP
<?php
function maximize( $a , $n )
{
$presum = array ();
$sum = 0;
$max_sum = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$presum [ $i ] = $max_sum ;
$max_sum += $a [ $i ];
$sum += $a [ $i ];
$max_sum = max( $max_sum , - $sum );
}
$ans = max( $sum , $max_sum );
$g = 0;
for ( $i = $n - 1; $i >= 0; -- $i )
{
$g -= $a [ $i ];
$ans = max( $ans , $g + $presum [ $i ]);
}
return $ans ;
}
$a = array (-4, 2, 0, 5, 0);
$n = count ( $a );
echo maximize( $a , $n );
?>
|
Javascript
<script>
function maximize(a , n) {
var presum = Array(n).fill(0);
var sum = 0;
var max_sum = 0;
for (i = 0; i < n; i++) {
presum[i] = max_sum;
max_sum += a[i];
sum += a[i];
max_sum = Math.max(max_sum, -sum);
}
var ans = Math.max(sum, max_sum);
var g = 0;
for (i = n - 1; i >= 0; --i) {
g -= a[i];
ans = Math.max(ans, g + presum[i]);
}
return ans;
}
var a = [ -4, 2, 0, 5, 0 ];
var n = a.length;
document.write(maximize(a, n));
</script>
|
Output:
11
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(N), as we are using extra space for presum array.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...