Related Articles

# Maximum subarray sum possible after removing at most one subarray

• Difficulty Level : Medium
• Last Updated : 29 May, 2021

Given an array arr[] consisting of N integers, the task is to find the maximum sum of any subarray possible after removing at most one subarray from the given array.

Examples:

Input: arr[] = {-1, 5, 2, -1, 6}
Output: 13
Explanation:
The maximum sum can be obtained by selecting the subarray {5, 2, -1, 6} and removing the subarray {-1}.

Input: arr[] = {7, 6, -1, -4, -5, 7, 1, -2, -3}
Output: 21

Approach: Follow the steps to solve the problem:

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to find maximum``// subarray sum possible after``// removing at most one subarray``void` `maximumSum(``int` `arr[], ``int` `n)``{``    ``int` `preSum[n];``    ``int` `sum = 0;``    ``int` `maxSum = 0;` `    ``// Calculate the preSum[] array``    ``for``(``int` `i = 0; i < n; i++)``    ``{``        ` `        ``// Update the value of sum``        ``sum = max(arr[i], sum + arr[i]);` `        ``// Update the value of maxSum``        ``maxSum = max(maxSum, sum);` `        ``// Update the value of preSum[i]``        ``preSum[i] = maxSum;``    ``}` `    ``sum = 0;``    ``maxSum = 0;` `    ``int` `postSum[n + 1];` `    ``// Calculate the postSum[] array``    ``for``(``int` `i = n - 1; i >= 0; i--)``    ``{``        ` `        ``// Update the value of sum``        ``sum = max(arr[i], sum + arr[i]);` `        ``// Update the value of maxSum``        ``maxSum = max(maxSum, sum);` `        ``// Update the value of postSum[i]``        ``postSum[i] = maxSum;``    ``}` `    ``// Stores the resultant maximum``    ``// sum of subarray``    ``int` `ans = 0;` `    ``for``(``int` `i = 0; i < n; i++)``    ``{``        ` `        ``// Update the value of ans``        ``ans = max(ans, preSum[i] + postSum[i]);``    ``}` `    ``// Print the resultant sum``    ``cout << (ans);``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 7, 6, -1, -4, -5, 7, 1 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);``    ` `    ``maximumSum(arr, n);``    ` `    ``return` `0;``}` `// This code is contributed by ukasp`

## Java

 `// Java program for the above approach``import` `java.io.*;``import` `java.util.*;` `class` `GFG {` `    ``// Function to find maximum``    ``// subarray sum possible after``    ``// removing at most one subarray``    ``public` `static` `void` `maximumSum(``        ``int` `arr[], ``int` `n)``    ``{``        ``long``[] preSum = ``new` `long``[n];` `        ``long` `sum = ``0``;``        ``long` `maxSum = ``0``;` `        ``// Calculate the preSum[] array``        ``for` `(``int` `i = ``0``; i < n; i++) {` `            ``// Update the value of sum``            ``sum = Math.max(arr[i],``                           ``sum + arr[i]);` `            ``// Update the value of maxSum``            ``maxSum = Math.max(maxSum,``                              ``sum);` `            ``// Update the value of preSum[i]``            ``preSum[i] = maxSum;``        ``}` `        ``sum = ``0``;``        ``maxSum = ``0``;` `        ``long``[] postSum = ``new` `long``[n + ``1``];` `        ``// Calculate the postSum[] array``        ``for` `(``int` `i = n - ``1``; i >= ``0``; i--) {` `            ``// Update the value of sum``            ``sum = Math.max(arr[i],``                           ``sum + arr[i]);` `            ``// Update the value of maxSum``            ``maxSum = Math.max(maxSum,``                              ``sum);` `            ``// Update the value of postSum[i]``            ``postSum[i] = maxSum;``        ``}` `        ``// Stores the resultant maximum``        ``// sum of subarray``        ``long` `ans = ``0``;` `        ``for` `(``int` `i = ``0``; i < n; i++) {` `            ``// Update the value of ans``            ``ans = Math.max(ans,``                           ``preSum[i]``                               ``+ postSum[i + ``1``]);``        ``}` `        ``// Print the resultant sum``        ``System.out.println(ans);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `arr[] = { ``7``, ``6``, -``1``, -``4``, -``5``, ``7``, ``1` `};``        ``maximumSum(arr, arr.length);``    ``}``}`

## Python3

 `# Python program for the above approach` `# Function to find maximum``# subarray sum possible after``# removing at most one subarray``def` `maximumSum(arr, n) :``            ` `    ``preSum ``=` `[``0``] ``*` `n`` ` `    ``sum` `=` `0``    ``maxSum ``=` `0` `    ``# Calculate the preSum[] array``    ``for` `i ``in` `range``(n):`` ` `        ``# Update the value of sum``        ``sum` `=` `max``(arr[i], ``sum` `+` `arr[i])`` ` `        ``# Update the value of maxSum``        ``maxSum ``=` `max``(maxSum,``                          ``sum``)`` ` `        ``# Update the value of preSum[i]``        ``preSum[i] ``=` `maxSum``        `  `    ``sum` `=` `0``    ``maxSum ``=` `0`` ` `    ``postSum ``=` `[``0``] ``*` `(n ``+` `1``)`` ` `    ``# Calculate the postSum[] array``    ``for` `i ``in` `range``(n``-``1``, ``-``1``, ``-``1``):``            ` `        ``# Update the value of sum``        ``sum` `=` `max``(arr[i],``                       ``sum` `+` `arr[i])`` ` `        ``# Update the value of maxSum``        ``maxSum ``=` `max``(maxSum,``                          ``sum``)`` ` `        ``# Update the value of postSum[i]``        ``postSum[i] ``=` `maxSum``        ` ` ` `    ``# Stores the resultant maximum``    ``# sum of subarray``    ``ans ``=` `0`` ` `    ``for` `i ``in` `range``(n):`` ` `        ``# Update the value of ans``        ``ans ``=` `max``(ans,``                       ``preSum[i]``                           ``+` `postSum[i ``+` `1``])``        ` ` ` `    ``# Print resultant sum``    ``print``(ans)``    ` `# Driver code``arr ``=` `[ ``7``, ``6``, ``-``1``, ``-``4``, ``-``5``, ``7``, ``1``]``N ``=` `len``(arr)``maximumSum(arr, N)` `# This code is contributed by sanjoy_62.`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `// Function to find maximum``// subarray sum possible after``// removing at most one subarray``public` `static` `void` `maximumSum(``int``[] arr, ``int` `n)``{``    ``long``[] preSum = ``new` `long``[n];``    ``long` `sum = 0;``    ``long` `maxSum = 0;` `    ``// Calculate the preSum[] array``    ``for``(``int` `i = 0; i < n; i++)``    ``{``        ` `        ``// Update the value of sum``        ``sum = Math.Max(arr[i], sum + arr[i]);` `        ``// Update the value of maxSum``        ``maxSum = Math.Max(maxSum, sum);` `        ``// Update the value of preSum[i]``        ``preSum[i] = maxSum;``    ``}` `    ``sum = 0;``    ``maxSum = 0;` `    ``long``[] postSum = ``new` `long``[n + 1];` `    ``// Calculate the postSum[] array``    ``for``(``int` `i = n - 1; i >= 0; i--)``    ``{``        ` `        ``// Update the value of sum``        ``sum = Math.Max(arr[i], sum + arr[i]);` `        ``// Update the value of maxSum``        ``maxSum = Math.Max(maxSum, sum);` `        ``// Update the value of postSum[i]``        ``postSum[i] = maxSum;``    ``}` `    ``// Stores the resultant maximum``    ``// sum of subarray``    ``long` `ans = 0;` `    ``for``(``int` `i = 0; i < n; i++)``    ``{``        ` `        ``// Update the value of ans``        ``ans = Math.Max(ans, preSum[i] +``                            ``postSum[i + 1]);``    ``}` `    ``// Print the resultant sum``    ``Console.WriteLine(ans);``}` `// Driver code``static` `void` `Main()``{``    ``int``[] arr = { 7, 6, -1, -4, -5, 7, 1 };``    ``maximumSum(arr, arr.Length);``}``}` `// This code is contributed by abhinavjain194`

## Javascript

 ``
Output:
`21`

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