The Wayback Machine - https://web.archive.org/web/20211005072706/https://www.geeksforgeeks.org/maximize-subarray-sum-by-inverting-sign-of-elements-of-any-subarray-at-most-twice/
Skip to content
Related Articles

Related Articles

Improve Article

Maximize subarray sum by inverting sign of elements of any subarray at most twice

  • Last Updated : 05 Oct, 2021

Given an array A of size n, find the maximum subarray sum after applying the given operation at most two times. In one operation, choose any two indices i and j and invert sign of all the elements from index i to index j, i.e, all positive elements in the range i to j become negative and all negative elements become positive.

Examples:

Input: A[] = {1, -2, -4, 3, 5, -6}
Output: 21
Explanation:
Inverting the array from index 1 to index 2 & from index 5 to index 5 (0-based indexing) to get {1, 2, 4, 3, 5, 6} with maximum subarray sum = 21

Input: A[] = {2, -1, -18, 3, -1, -39, 5, -2}
Output: 69

Approach: The idea is to first of all, merge all elements into groups. i.e all consecutive positive elements will be summed to one integer and will be placed in a new array arr and similarly for consecutive negative elements. After that, the problem reduces to find the maximum subarray sum after inverting at most two elements from this array.



The idea is to use Dynamic Programming. Now, create a dp[n][3] array and Here at every index in that dp array, maintain three numbers:

  • The first number represents the maximum subarray sum after inverting the no element in the array. So, dp[i][0] will simply run the logic of kadane’s algorithm for the maximum subarray sum.
  • The second number represents the maximum subarray sum after inverting exactly one element in the array. So, dp[i][1] will be the maximum of two values i.e dp[i-1][1] + arr[i] (maximum subarray sum after inverting one element till i and then adding the current element to it) and dp[i][0]+ (-arr[i]) ( i.e the previous non-inverted maximum subarray sum till i-1 and then adding current element after inverting ).
  • The third number represents the maximum subarray sum after inverting exactly two elements in the array. So, dp[i][2] will be maximum of two values i.e     (maximum subarray sum after inverting one element till i-1 and then adding the current element after inverting) and     (maximum subarray sum after inverting two elements till i-1 and adding the current element to it).
  • Keep a maxSum variable which will be updated after every index and will be equal to the maximum of the previous value of maxSum and all three current values i.e. dp[i][0], dp[i][1] and dp[i][2].
  • Return maxSum, which is the answer.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to merge alternate elements into groups
// of positive and negative
void mergeElements(int n, vector<int>& arr, int* A)
{
    int i = 0;
    int sum = 0;
    while (i < n) {
        sum = 0;
        while (i < n && A[i] >= 0) {
            sum += A[i];
            i++;
        }
        if (sum > 0) {
            arr.push_back(sum);
        }
        sum = 0;
        while (i < n && A[i] < 0) {
            sum += A[i];
            i++;
        }
        if (sum < 0) {
            arr.push_back(sum);
        }
    }
}
  
// Function to return the maximum
// after inverting at most 2 elements
int findMaxSum(vector<int>& arr, int n)
{
    int maxSum = 0;
    vector<vector<int> > dp(
n,
 vector<int>(3, INT_MIN));
  
    dp[0][0] = max(0, arr[0]);
    dp[0][1] = -1 * arr[0];
    for (int i = 1; i < n; ++i) {
  
        // dp[i][0] represents sum till ith index
        // without inverting any element.
        dp[i][0] = max(arr[i], 
dp[i - 1][0] + arr[i]);
  
        // dp[i][1] represents sum till ith index
        // after inverting one element.
        dp[i][1] = max(0, dp[i - 1][0]) - arr[i];
        if (i >= 1) {
            dp[i][1] = max(dp[i][1], 
dp[i - 1][1] + arr[i]);
  
            // dp[i][2] represents sum till ith index
            // after inverting two elements.
            dp[i][2] = dp[i - 1][1] - arr[i];
        }
  
        if (i >= 2) {
            dp[i][2] = max(dp[i][2], 
dp[i - 1][2] + arr[i]);
        }
        maxSum = max(maxSum, dp[i][0]);
        maxSum = max(maxSum, dp[i][1]);
        maxSum = max(maxSum, dp[i][2]);
    }
    return maxSum;
}
  
// Driver Code
int main()
{
    int n = 8;
    int A[8] = { 2, -1, -18, 3, -1, -39, 5, -2 };
  
    // vector 'arr' contains sum of consecutive
    // positive or negative elements.
    vector<int> arr;
    mergeElements(n, arr, A);
  
    cout << findMaxSum(arr, arr.size());
    return 0;
}
Output
69

Time Complexity: O(N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :