Maximum sum of all elements of array after performing given operations
Last Updated :
12 Sep, 2022
Given an array of integers. The task is to find the maximum sum of all the elements of the array after performing the given two operations once each.
The operations are:
1. Select some(possibly none) continuous elements from the beginning of the array and multiply by -1.
2. Select some(possibly none) continuous elements from the end of the array and multiply by -1.
Examples:
Input : arr[] = {-1, 10, -5, 10, -2}
Output : 18
After 1st operation : 1 10 -5 10 -2
After 2nd operation : 1 10 -5 10 2
Input : arr[] = {-9, -8, -7}
Output : 24
After 1st operation : 9 8 -7
After 2nd operation : 9 8 7
Approach: This problem can be solved in linear time, using the following idea:
- Let the sum of elements A1 .. An be equal to S. Then when inverting signs we get -A1, -A2 .. -An, and the sum is thereafter changed to -S, i.e. sum of elements on the segment will just change its’ sign when inverting the whole segment’s signs.
- Consider the initial problem as follows: choose a consecutive subsequence, and invert all the numbers remaining out of it.
- Find the Maximum subarray sum using Kadane’ Algorithm.
- Keep that subarray intact and multiply the rest with -1.
- Considering the sum of the whole array as S, and the largest sum contiguous subarray as S1, the total sum will be equal to -(S-S1) + S1 = 2*S1 – S. This is the required sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxSubArraySum( int a[], int size)
{
int max_so_far = INT_MIN, max_ending_here = 0;
for ( int i = 0; i < size; i++) {
max_ending_here = max_ending_here + a[i];
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
if (max_ending_here < 0)
max_ending_here = 0;
}
return max_so_far;
}
int maxSum( int a[], int n)
{
int S = 0;
int S1 = maxSubArraySum(a, n);
for ( int i = 0; i < n; i++)
S += a[i];
return (2 * S1 - S);
}
int main()
{
int a[] = { -35, 32, -24, 0, 27, -10, 0, -19 };
int n = sizeof (a) / sizeof (a[0]);
cout << maxSum(a, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int maxSubArraySum( int a[], int size)
{
int max_so_far = Integer.MIN_VALUE, max_ending_here = 0 ;
for ( int i = 0 ; i < size; i++) {
max_ending_here = max_ending_here + a[i];
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
if (max_ending_here < 0 )
max_ending_here = 0 ;
}
return max_so_far;
}
static int maxSum( int a[], int n)
{
int S = 0 ;
int S1 = maxSubArraySum(a, n);
for ( int i = 0 ; i < n; i++)
S += a[i];
return ( 2 * S1 - S);
}
public static void main (String[] args) {
int a[] = { - 35 , 32 , - 24 , 0 , 27 , - 10 , 0 , - 19 };
int n = a.length;
System.out.println( maxSum(a, n));
}
}
|
Python3
import sys
def maxSubArraySum(a, size) :
max_so_far = - (sys.maxsize - 1 )
max_ending_here = 0
for i in range (size) :
max_ending_here = max_ending_here + a[i]
if (max_so_far < max_ending_here) :
max_so_far = max_ending_here
if (max_ending_here < 0 ) :
max_ending_here = 0
return max_so_far
def maxSum(a, n) :
S = 0 ;
S1 = maxSubArraySum(a, n)
for i in range (n) :
S + = a[i]
return ( 2 * S1 - S)
if __name__ = = "__main__" :
a = [ - 35 , 32 , - 24 , 0 ,
27 , - 10 , 0 , - 19 ]
n = len (a)
print (maxSum(a, n))
|
C#
using System;
class GFG {
static int maxSubArraySum( int []a, int size)
{
int max_so_far = int .MinValue, max_ending_here = 0;
for ( int i = 0; i < size; i++) {
max_ending_here = max_ending_here + a[i];
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
if (max_ending_here < 0)
max_ending_here = 0;
}
return max_so_far;
}
static int maxSum( int []a, int n)
{
int S = 0;
int S1 = maxSubArraySum(a, n);
for ( int i = 0; i < n; i++)
S += a[i];
return (2 * S1 - S);
}
public static void Main () {
int []a = { -35, 32, -24, 0, 27, -10, 0, -19 };
int n = a.Length;
Console.WriteLine( maxSum(a, n));
}
}
|
PHP
<?php
function maxSubArraySum( $a , $size )
{
$max_so_far = PHP_INT_MIN;
$max_ending_here = 0;
for ( $i = 0; $i < $size ; $i ++)
{
$max_ending_here = $max_ending_here + $a [ $i ];
if ( $max_so_far < $max_ending_here )
$max_so_far = $max_ending_here ;
if ( $max_ending_here < 0)
$max_ending_here = 0;
}
return $max_so_far ;
}
function maxSum( $a , $n )
{
$S = 0;
$S1 = maxSubArraySum( $a , $n );
for ( $i = 0; $i < $n ; $i ++)
$S += $a [ $i ];
return (2 * $S1 - $S );
}
$a = array (-35, 32, -24, 0,
27, -10, 0, -19);
$n = sizeof( $a );
echo ( maxSum( $a , $n ));
|
Javascript
<script>
function maxSubArraySum(a , size)
{
var max_so_far = Number.MIN_VALUE, max_ending_here = 0;
for (i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
if (max_ending_here < 0)
max_ending_here = 0;
}
return max_so_far;
}
function maxSum(a, n)
{
var S = 0;
var S1 = maxSubArraySum(a, n);
for (i = 0; i < n; i++)
S += a[i];
return (2 * S1 - S);
}
var a = [ -35, 32, -24, 0, 27, -10, 0, -19 ];
var n = a.length;
document.write(maxSum(a, n));
</script>
|
Complexity Analysis:
- Time Complexity: O(n), where n represents the size of the given array.
- Auxiliary Space: O(1), no extra space is required, so it is a constant.
Share your thoughts in the comments
Please Login to comment...