Maximize the maximum subarray sum after removing atmost one element Last Updated : 13 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Given an array arr[] of N integers, the task is to find the maximum sum of a subarray with at most one deletion allowed. The size of subarray to be considered should be at least 1.Examples: Input: arr[] = {1, 2, 3, -2, 3} Output: 9 The maximum sub-array sum is given by the sub-array {2, 3, -2, 3} Hence, we can remove -2 to further maximize the sub-array sum. Input: arr[] = {-1, -2} Output: -1 The maximum sub-array sum is from the sub-array {-1} and no removal is required. Approach: Idea is based upon Kadane's algorithm to find the maximum subarray sum. In the standard Kadane's algorithm, we traverse the array from left to right and keep track of maximum sum ending with every element. We finally return the maximum of all max ending here sums. Here we modify the algorithm to keep track of the maximum sum ending here with one deletion allowed. C++ #include <iostream> using namespace std; int maximizeSum(int arr[], int n) { // Max sum ending here with // at most one deletion int maxEHereDel = arr[0]; // Max sum ending here // without any deletion int mEhere = arr[0]; // To store result int res = arr[0]; for (int i=1; i<n; i++) { int x = arr[i]; // a) Start a new subarray // b) Exclude current element // c) Include current element maxEHereDel = max(x, max(mEhere, maxEHereDel+x)); // Updating maxEHere (without any deletion) // a) Begin a new subarray // b) Continue with the prev one mEhere = max(x, mEhere + x); res = max(res, maxEHereDel); } return res; } // Driver code int main() { int a[] = { 10, -100, -1, 10, -5, 4 }; int n = sizeof(a) / sizeof(a[0]); cout << maximizeSum(a, n); return 0; } Output19Time Complexity: O(n)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Maximize the maximum subarray sum after removing atmost one element S Striver Follow Improve Article Tags : Misc Mathematical DSA Arrays subarray subarray-sum +2 More Practice Tags : ArraysMathematicalMisc Similar Reads Maximum subarray sum possible after removing at most K array elements Given an array arr[] of size N and an integer K, the task is to find the maximum subarray sum by removing at most K elements from the array. Examples: Input: arr[] = { -2, 1, 3, -2, 4, -7, 20 }, K = 1 Output: 26 Explanation: Removing arr[5] from the array modifies arr[] to { -2, 1, 3, -2, 4, 20 } Su 15+ min read Maximize the sum of maximum elements of at least K-sized subarrays Given an integer array arr[] of length N and an integer K, partition the array in some non-overlapping subarrays such that each subarray has size at least K and each element of the array should be part of a subarray. The task is to maximize the sum of maximum elements across all the subarrays. Examp 7 min read Maximize product of subarray sum with its maximum element Given an array arr[] consisting of N positive integers, the task is to find the maximum product of the subarray sum with the maximum element of that subarray. Examples: Input: arr[] = {2, -3, 8, -2, 5}Output: 88Explanation:The required maximum product can be obtained using subarray {8, -2, 5}Therefo 8 min read Maximize Array sum by replacing middle elements with min of Subarray corners Given an array A[] of length N. Then your task is to output the maximum sum that can be achieved by using the given operation at any number of times (possibly zero): Select a subarray, whose length is at least 3.Replace all the middle elements (Except the first and last) with a minimum of elements a 7 min read Maximize product of subarray sum with its minimum element Given an array arr[] consisting of N positive integers, the task is to find the maximum product of subarray sum with the minimum element of that subarray. Examples: Input: arr[] = {3, 1, 6, 4, 5, 2}Output: 60Explanation:The required maximum product can be obtained using subarray {6, 4, 5}Therefore, 10 min read Like