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++ // CPP program to find maximum array sum // with multiplications of a prefix and a // suffix with -1 allowed. #include <iostream> using namespace std; // function to maximize the sum int maximize(int a[], int n) { // stores the pre sum int presum[n]; // to store sum from 0 to i int sum = 0; // stores the maximum sum with // prefix multiplication with -1. int max_sum = 0; // traverse from 0 to n for (int i = 0; i<n ; i++) { // calculate the presum presum[i] = max_sum ; // calculate sum max_sum += a[i]; sum += a[i]; max_sum = max(max_sum, -sum); } // Initialize answer. int ans = max(sum, max_sum); // traverse from back to start int g = 0; for (int i = n-1; i >= 0; --i) { // stores the sum multiplied by (-1) g -= a[i]; // stores the max of ans and // presum + (-1*negative sum); ans = max(ans, g + presum[i]); } // returns answer return ans; } // driver program to test the above function int main() { int a[] = {-4, 2, 0, 5, 0}; int n = sizeof(a)/sizeof(a[0]); cout << maximize(a, n); return 0; }
Java // JAVA program to find maximum array sum // with multiplications of a prefix and a // suffix with -1 allowed. import java.math.*; class GFG { // function to maximize the sum static int maximize(int a[], int n) { // stores the pre sum int presum[] =new int[n]; // to store sum from 0 to i int sum = 0; // stores the maximum sum with // prefix multiplication with -1. int max_sum = 0; // traverse from 0 to n for (int i = 0; i<n ; i++) { // calculate the presum presum[i] = max_sum ; // calculate sum max_sum += a[i]; sum += a[i]; max_sum = Math.max(max_sum, -sum); } // Initialize answer. int ans = Math.max(sum, max_sum); // traverse from back to start int g = 0; for (int i = n-1; i >= 0; --i) { // stores the sum multiplied by (-1) g -= a[i]; // stores the max of ans and // presum + (-1*negative sum); ans = Math.max(ans, g + presum[i]); } // returns answer return ans; } // driver program to test the above function public static void main(String args[]) { int a[] = {-4, 2, 0, 5, 0}; int n = a.length; System.out.println(maximize(a, n)); } } /*This code is contributed by Nikita Tiwari.*/
Python3 # Python 3 program to find maximum array # sum with multiplications of a prefix # and a suffix with -1 allowed. # function to maximize the sum def maximize(a,n) : # stores the pre sum presum = [0] * n # to store sum from 0 to i sm = 0 # stores the maximum sum with # prefix multiplication with -1. max_sum = 0 # traverse from 0 to n for i in range(0,n) : # calculate the presum presum[i] = max_sum # calculate sum max_sum =max_sum + a[i] sm = sm + a[i] max_sum = max(max_sum, -sm) # Initialize answer. ans = max(sm, max_sum) # traverse from back to start g = 0 for i in range(n-1,-1,-1) : # stores the sum multiplied by (-1) g = g - a[i] # stores the max of ans and # presum + (-1*negative sum); ans = max(ans, g + presum[i]) # returns answer return ans # driver program to test the above function a = [-4, 2, 0, 5, 0] n = len(a) print(maximize(a, n)) #This code is contributed by Nikita Tiwari.
C# // C# program to find maximum array sum // with multiplications of a prefix and a // suffix with -1 allowed. using System; class GFG { // function to maximize the sum static int maximize(int []a, int n) { // stores the pre sum int []presum =new int[n]; // to store sum from 0 to i int sum = 0; // stores the maximum sum with // prefix multiplication with -1. int max_sum = 0; // traverse from 0 to n for (int i = 0; i < n ; i++) { // calculate the presum presum[i] = max_sum ; // calculate sum max_sum += a[i]; sum += a[i]; max_sum = Math.Max(max_sum, -sum); } // Initialize answer. int ans = Math.Max(sum, max_sum); // traverse from back to start int g = 0; for (int i = n - 1; i >= 0; --i) { // stores the sum multiplied by (-1) g -= a[i]; // stores the max of ans and // presum + (-1*negative sum); ans = Math.Max(ans, g + presum[i]); } // returns answer return ans; } // Driver Code public static void Main() { int []a = {-4, 2, 0, 5, 0}; int n = a.Length; Console.WriteLine(maximize(a, n)); } } // This code is contributed by vt_m.
PHP <?php // PHP program to find maximum array sum // with multiplications of a prefix and a // suffix with -1 allowed. // function to maximize the sum function maximize($a, $n) { // stores the pre sum $presum = array(); // to store sum // from 0 to i $sum = 0; // stores the maximum // sum with prefix // multiplication with -1. $max_sum = 0; // traverse from 0 to n for ($i = 0; $i < $n ; $i++) { // calculate the presum $presum[$i] = $max_sum ; // calculate sum $max_sum += $a[$i]; $sum += $a[$i]; $max_sum = max($max_sum, -$sum); } // Initialize answer. $ans = max($sum, $max_sum); // traverse from // back to start $g = 0; for ($i = $n - 1; $i >= 0; --$i) { // stores the sum // multiplied by (-1) $g -= $a[$i]; // stores the max of ans and // presum + (-1*negative sum); $ans = max($ans, $g + $presum[$i]); } // returns answer return $ans; } // Driver Code $a = array(-4, 2, 0, 5, 0); $n = count($a); echo maximize($a, $n); // This code is contributed by anuj_67. ?>
JavaScript <script> // javascript program to find maximum array sum // with multiplications of a prefix and a // suffix with -1 allowed. // function to maximize the sum function maximize(a , n) { // stores the pre sum var presum = Array(n).fill(0); // to store sum from 0 to i var sum = 0; // stores the maximum sum with // prefix multiplication with -1. var max_sum = 0; // traverse from 0 to n for (i = 0; i < n; i++) { // calculate the presum presum[i] = max_sum; // calculate sum max_sum += a[i]; sum += a[i]; max_sum = Math.max(max_sum, -sum); } // Initialize answer. var ans = Math.max(sum, max_sum); // traverse from back to start var g = 0; for (i = n - 1; i >= 0; --i) { // stores the sum multiplied by (-1) g -= a[i]; // stores the max of ans and // presum + (-1*negative sum); ans = Math.max(ans, g + presum[i]); } // returns answer return ans; } // driver program to test the above function var a = [ -4, 2, 0, 5, 0 ]; var n = a.length; document.write(maximize(a, n)); // This code is contributed by todaysgaurav </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.
Similar Reads
Maximize the sum of array after multiplying a prefix and suffix by -1
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:
7 min read
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
Index with Minimum sum of prefix and suffix sums in an Array
Given an array of integers. The task is to find the index [Tex]i [/Tex]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 f
10 min read
Maximum sum of pairwise product in an array with negative allowed
Given an array of n elements. Find maximum sum of pairwise multiplications. Sum can be larger so take mod with 10^9+7. If there are odd elements, then we can add any one element (without forming a pair) to the sum. Examples: Input : arr[] = {-1, 4, 5, -7, -4, 9, 0} Output : 77 So to get the maximum
9 min read
Minimize the maximum subarray sum with 1s and -2s
Given two integers X and Y. X and Y represent the frequency of elements 1 and -2 you have. You have to arrange all elements such that the maximum sum over all subarrays is minimized, and then find the maximum subarray sum of such rearrangement. Examples: Input: X = 1, Y = 1Output: 1Explanation: X =
5 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
Find the element whose multiplication with -1 makes array sum 0
Given an array of N integers. The task is to find the smallest index of an element such that when multiplied by -1 the sum of whole array becomes 0. If there is no such index return -1. Examples: Input : arr[] = {1, 3, -5, 3, 4}Output : 2Input : arr[] = {5, 3, 6, -7, -4}Output : -1Naive Approach: Th
9 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 number of indices with prefix sum equal to 0
Given an array arr[] of size N, you can perform the following operation on the array any number of times: If arr[i]=0, we can replace it with any integer. Your task is to maximize the number of indices having prefix sum = 0. Examples: Input: N = 7, arr[] = {1, -1, 0, 2, 4, 2, 0, 1}Output: 3Explanati
10 min read
Maximum prefix sum which is equal to suffix sum such that prefix and suffix do not overlap
Given an array arr[] of N Positive integers, the task is to find the largest prefix sum which is also the suffix sum and prefix and suffix do not overlap. Examples: Input: N = 5, arr = [1, 3, 2, 1, 4]Output: 4Explanation: consider prefix [1, 3] and suffix [4] which gives maximum prefix sum which is
7 min read