Maximize the sum of array after multiplying a prefix and suffix by -1
Last Updated :
13 Dec, 2022
Given an array arr[] of length N, the task is to maximize the sum of all the elements of the array by performing the following operations at most once.
- Choose a prefix of the array and multiply all the elements by -1.
- Choose a suffix of the array and multiply all the elements by -1.
Examples:
Input: arr[] = {-1, -2, -3}
Output: 6
Explanation:
Operation 1: Prefix of array - {-1, -2, -3}
can be multiplied with -1 to get the maximum sum.
Array after Operation: {1, 2, 3}
Sum = 1 + 2 + 3 = 6
Input: arr[] = {-4, 2, 0, 5, 0}
Output: 11
Explanation:
Operation 1: Prefix of array - {-4}
can be multiplied with -1 to get the maximum sum.
Array after operation: {4, 2, 0, 5, 0}
Sum = 4 + 2 + 0 + 5 + 0 = 11
Approach: The key observation in the problem is if the chosen range of the prefix and suffix intersect, then the elements of intersection portion have same sign. Due to which it is always better to choose non-intersecting ranges of the prefix and suffix array. Below is the illustration of the steps:
- It is easily been observed that there will be a portion/subarray in the array whose sum is the same as the original and the sum of the other elements is reversed. So the new sum of the array will be:
// X - Sum of subarray which is not in
// the range of the prefix and suffix
// S - Sum of the original array
New Sum = X + -1*(S - X) = 2*X - S
- Hence, the idea is to maximize the value of X to get the maximum sum because S is the constant value which cannot be changed. This can be achieved with the help of the Kadane's Algorithm.
Below is the implementation of the above approach:
C++
// C++ implementation to find the
// maximum sum of the array by
// multiplying the prefix and suffix
// of the array by -1
#include <bits/stdc++.h>
using namespace std;
// Kadane's algorithm to find
// the maximum subarray sum
int maxSubArraySum(int a[], int size)
{
int max_so_far = INT_MIN,
max_ending_here = 0;
// Loop to find the maximum subarray
// array sum in the given array
for (int i = 0; i < size; i++) {
max_ending_here =
max_ending_here + a[i];
if (max_ending_here < 0)
max_ending_here = 0;
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
}
return max_so_far;
}
// Function to find the maximum
// sum of the array by multiplying
// the prefix and suffix by -1
int maxSum(int a[], int n)
{
// Total initial sum
int S = 0;
// Loop to find the maximum
// sum of the array
for (int i = 0; i < n; i++)
S += a[i];
int X = maxSubArraySum(a, n);
// Maximum value
return 2 * X - S;
}
// Driver Code
int main()
{
int a[] = { -1, -2, -3 };
int n = sizeof(a) / sizeof(a[0]);
int max_sum = maxSum(a, n);
cout << max_sum;
return 0;
}
Java
// Java implementation to find the
// maximum sum of the array by
// multiplying the prefix and suffix
// of the array by -1
import java.io.*;
class GFG
{
// Kadane's algorithm to find
// the maximum subarray sum
static int maxSubArraySum(int a[], int size)
{
int max_so_far = Integer.MIN_VALUE,
max_ending_here = 0;
// Loop to find the maximum subarray
// array sum in the given array
for (int i = 0; i < size; i++) {
max_ending_here =
max_ending_here + a[i];
if (max_ending_here < 0)
max_ending_here = 0;
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
}
return max_so_far;
}
// Function to find the maximum
// sum of the array by multiplying
// the prefix and suffix by -1
static int maxSum(int a[], int n)
{
// Total initial sum
int S = 0;
int i;
// Loop to find the maximum
// sum of the array
for (i = 0; i < n; i++)
S += a[i];
int X = maxSubArraySum(a, n);
// Maximum value
return 2 * X - S;
}
// Driver Code
public static void main(String []args)
{
int a[] = { -1, -2, -3 };
int n = a.length;
int max_sum = maxSum(a, n);
System.out.print(max_sum);
}
}
// This code is contributed by chitranayal
Python3
# Python3 implementation to find the
# maximum sum of the array by
# multiplying the prefix and suffix
# of the array by -1
# Kadane's algorithm to find
# the maximum subarray sum
def maxSubArraySum(a, size):
max_so_far = -10**9
max_ending_here = 0
# Loop to find the maximum subarray
# array sum in the given array
for i in range(size):
max_ending_here = max_ending_here + a[i]
if (max_ending_here < 0):
max_ending_here = 0
if (max_so_far < max_ending_here):
max_so_far = max_ending_here
return max_so_far
# Function to find the maximum
# sum of the array by multiplying
# the prefix and suffix by -1
def maxSum(a, n):
# Total initial sum
S = 0
# Loop to find the maximum
# sum of the array
for i in range(n):
S += a[i]
X = maxSubArraySum(a, n)
# Maximum value
return 2 * X - S
# Driver Code
if __name__ == '__main__':
a=[-1, -2, -3]
n= len(a)
max_sum = maxSum(a, n)
print(max_sum)
# This code is contributed by mohit kumar 29
C#
// C# implementation to find the
// maximum sum of the array by
// multiplying the prefix and suffix
// of the array by -1
using System;
class GFG
{
// Kadane's algorithm to find
// the maximum subarray sum
static int maxSubArraySum(int []a, int size)
{
int max_so_far = int.MinValue,
max_ending_here = 0;
// Loop to find the maximum subarray
// array sum in the given array
for (int i = 0; i < size; i++) {
max_ending_here =
max_ending_here + a[i];
if (max_ending_here < 0)
max_ending_here = 0;
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
}
return max_so_far;
}
// Function to find the maximum
// sum of the array by multiplying
// the prefix and suffix by -1
static int maxSum(int []a, int n)
{
// Total initial sum
int S = 0;
int i;
// Loop to find the maximum
// sum of the array
for (i = 0; i < n; i++)
S += a[i];
int X = maxSubArraySum(a, n);
// Maximum value
return 2 * X - S;
}
// Driver Code
public static void Main(String []args)
{
int []a = { -1, -2, -3 };
int n = a.Length;
int max_sum = maxSum(a, n);
Console.Write(max_sum);
}
}
// This code is contributed by sapnasingh4991
JavaScript
<script>
// Javascript implementation to find the
// maximum sum of the array by
// multiplying the prefix and suffix
// of the array by -1
// Kadane's algorithm to find
// the maximum subarray sum
function maxSubArraySum(a, size)
{
var max_so_far = Number.MIN_VALUE,
max_ending_here = 0;
// Loop to find the maximum subarray
// array sum in the given array
for(i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if (max_ending_here < 0)
max_ending_here = 0;
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
}
return max_so_far;
}
// Function to find the maximum
// sum of the array by multiplying
// the prefix and suffix by -1
function maxSum(a, n)
{
// Total initial sum
var S = 0;
var i;
// Loop to find the maximum
// sum of the array
for(i = 0; i < n; i++)
S += a[i];
var X = maxSubArraySum(a, n);
// Maximum value
return 2 * X - S;
}
// Driver Code
var a = [ -1, -2, -3 ];
var n = a.length;
var max_sum = maxSum(a, n);
document.write(max_sum);
// This code is contributed by aashish1995
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Maximize the sum of array by multiplying prefix of array with -1 Given an array of elements 'arr', the task is to maximize the sum of the elements of this array after performing the following operation: You can take any prefix of 'arr' and multiply each element of the prefix with '-1'. In the first line, print the maximized sum than in the next line, print the in
7 min read
Maximum array sum with prefix and suffix multiplications with -1 allowed 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
8 min read
Index with Minimum sum of prefix and suffix sums in an Array Given an array of integers. The task is to find the index i in the array at which the value of prefixSum(i) + suffixSum(i) is minimum.Note: PrefixSum(i) = The sum of first i numbers of the array.SuffixSum(i) = the sum of last N - i + 1 numbers of the array.1-based indexing is considered for the arra
10 min read
Maximize deletions by removing prefix and suffix of Array with same sum Given an array Arr[] of size N, the cost of removing ith element is Arr[i]. The task is to remove the maximum number of elements by removing the prefix and the suffix of the same length and having the same total cost. Examples: Input: Arr[] = {80, 90, 81, 80}Output: 2 Explanation: If we choose 80 fr
10 min read
Maximize array sum by replacing equal adjacent pairs by their sum and X respectively Given two integers N and X which denotes the size of an array arr[] and the initial value of all the array elements respectively, the task is to find the maximum sum possible from the given array after performing the following operation any number of times. Choose any valid index i for which arr[i]
6 min read
Maximize array sum by alternating the signs of adjacent elements Given an array, arr[] of size N, the task is to find the maximum possible sum of array elements by alternating the signs of adjacent array elements. Examples: Input: arr[] = { -2, 1, 0 } Output: 3 Explanation: Alternating the signs of (arr[0], arr[1]) modifies arr[] to {2, -1, 0}. Alternating the si
7 min read
Maximum prefix sum after K reversals of a given array Given an array arr[] of size N and a positive integer K, the task is to find the maximum prefix sum after K reversals of the given array. Examples: Input: arr[] = {1, 5, 8, 9, 11, 2}, K = 1Output: 36Explanation: Reverse the array once. Therefore, the array becomes {2, 11, 9, 8, 5, 1}. Maximum prefix
9 min read
Rearrange array elements to maximize the sum of MEX of all prefix arrays Given an array arr[] of size N, the task is to rearrange the array elements such that the sum of MEX of all prefix arrays is the maximum possible. Note: MEX of a sequence is the minimum non-negative number not present in the sequence. Examples: Input: arr[] = {2, 0, 1}Output: 0, 1, 2Explanation:Sum
7 min read
Maximum possible array sum after performing the given operation Given an array arr[] of size N, the task is to find the maximum sum of the elements of the array after applying the given operation any number of times. In a single operation, choose an index 1 ? i < N and multiply both arr[i] and arr[i - 1] by -1.Examples: Input: arr[] = {-10, 5, -4} Output: 19
9 min read