Maximum sum of the array after dividing it into three segments
Last Updated :
21 Jun, 2022
Given an array a of size N. The task is to find the maximum sum of array possible by dividing the array into three segments such that each element in the first segment is multiplied by -1 and each element in the second segment is multiplied by 1 and each element in the third segment is multiplied by -1. The segments can intersect and any segment may include zero in it.
Examples:
Input : a[] = {-6, 10, -3, 10, -2}
Output : 25
Divide the segments as {-6}, {10, -3, 10}, {-2)
Input : a[] = {-6, -10}
Output : 16
Approach:
First we need is to calculate for all possible situations for every ith element where the division should be made.
- In the first traversal find if the ith element produces maximum sum by multiplying with -1 or keeping it as it is.
- Store all values in the array b.
- In the second traversal find maximum sum by decreasing a[i] and adding b[i] to it.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int Max_Sum( int a[], int n)
{
int b[n];
int S = 0;
int res = 0;
for ( int i = 0; i < n; i++)
{
b[i] = res;
res += a[i];
S += a[i];
res = max(res, -S);
}
int ans = S;
ans = max(ans, res);
int g = 0;
for ( int i = n - 1; i >= 0; --i) {
g -= a[i];
ans = max(ans, g + b[i]);
}
return ans;
}
int main()
{
int a[] = { -6, 10, -3, 10, -2 };
int n = sizeof (a) / sizeof (a[0]);
cout << "Maximum sum is: " << Max_Sum(a, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int Max_Sum( int a[], int n)
{
int []b = new int [n];
int S = 0 ;
int res = 0 ;
for ( int i = 0 ; i < n; i++)
{
b[i] = res;
res += a[i];
S += a[i];
res = Math.max(res, -S);
}
int ans = S;
ans = Math.max(ans, res);
int g = 0 ;
for ( int i = n - 1 ; i >= 0 ; --i)
{
g -= a[i];
ans = Math.max(ans, g + b[i]);
}
return ans;
}
public static void main(String[] args)
{
int a[] = { - 6 , 10 , - 3 , 10 , - 2 };
int n = a.length;
System.out.println( "Maximum sum is: " +
Max_Sum(a, n));
}
}
|
Python3
def Max_Sum(a, n):
b = [ 0 for i in range (n)]
S = 0
res = 0
for i in range (n):
b[i] = res
res + = a[i]
S + = a[i]
res = max (res, - S)
ans = S
ans = max (ans, res)
g = 0
for i in range (n - 1 , - 1 , - 1 ):
g - = a[i]
ans = max (ans, g + b[i])
return ans
a = [ - 6 , 10 , - 3 , 10 , - 2 ]
n = len (a)
print ( "Maximum sum is:" ,
Max_Sum(a, n))
|
C#
using System;
class GFG
{
static int Max_Sum( int []a, int n)
{
int []b = new int [n];
int S = 0;
int res = 0;
for ( int i = 0; i < n; i++)
{
b[i] = res;
res += a[i];
S += a[i];
res = Math.Max(res, -S);
}
int ans = S;
ans = Math.Max(ans, res);
int g = 0;
for ( int i = n - 1; i >= 0; --i)
{
g -= a[i];
ans = Math.Max(ans, g + b[i]);
}
return ans;
}
public static void Main()
{
int []a = { -6, 10, -3, 10, -2 };
int n = a.Length;
Console.WriteLine( "Maximum sum is: " +
Max_Sum(a, n));
}
}
|
PHP
<?php
function Max_Sum( $a , $n )
{
$b = array ();
$S = 0;
$res = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$b [ $i ] = $res ;
$res += $a [ $i ];
$S += $a [ $i ];
$res = max( $res , - $S );
}
$ans = $S ;
$ans = max( $ans , $res );
$g = 0;
for ( $i = $n - 1; $i >= 0; -- $i )
{
$g -= $a [ $i ];
$ans = max( $ans , $g + $b [ $i ]);
}
return $ans ;
}
$a = array (-6, 10, -3, 10, -2 );
$n = count ( $a );
echo ( "Maximum sum is: " );
echo Max_Sum( $a , $n );
?>
|
Javascript
<script>
function Max_Sum(a, n)
{
let b = new Array(n);
let S = 0;
let res = 0;
for (let i = 0; i < n; i++)
{
b[i] = res;
res += a[i];
S += a[i];
res = Math.max(res, -S);
}
let ans = S;
ans = Math.max(ans, res);
let g = 0;
for (let i = n - 1; i >= 0; --i)
{
g -= a[i];
ans = Math.max(ans, g + b[i]);
}
return ans;
}
let a = [ -6, 10, -3, 10, -2 ];
let n = a.length;
document.write( "Maximum sum is: " +
Max_Sum(a, n));
</script>
|
Output:
Maximum sum is: 25
Complexity Analysis:
Time complexity: O(N), as we are having 2 passes of the loop of size N inside the Max_Sum() function, so time complexity will be O(N+N)->O(N).
Space Complexity: O(N), as we have created an array of size N inside the Max_Sum() function.
Share your thoughts in the comments
Please Login to comment...