
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximize the maximum subarray sum after removing at most one element in C++
Problem statement
Given an array arr[] of N integers. The task is to first find the maximum sub-array sum and then remove at most one element from the sub-array. Remove at most a single element such that the maximum sum after removal is maximized.
If given input array is {1, 2, 3, -2, 3} then maximum sub-array sum is {2, 3, -2, 3}. Then we can remove -2. After removing the remaining array becomes−
{1, 2, 3, 3} with sum 9 which is maximum.
Algorithm
1. Use Kadane’s algorithm to find the maximum subarray sum. 2. Once the sum has beens find, re-apply Kadane’s algorithm to find the maximum sum again with some minor changes)
Example
#include <bits/stdc++.h> using namespace std; int getMaxSubarraySum(int *arr, int n){ int max = INT_MIN; int currentMax = 0; for (int i = 0; i < n; ++i) { currentMax = currentMax + arr[i]; if (max < currentMax) { max = currentMax; } if (currentMax < 0) { currentMax = 0; } } return max; } int getMaxSum(int *arr, int n){ int cnt = 0; int minVal = INT_MAX; int minSubarr = INT_MAX; int sum = getMaxSubarraySum(arr, n); int max = INT_MIN; int currentMax = 0; for (int i = 0; i < n; ++i) { currentMax = currentMax + arr[i]; ++cnt; minSubarr = min(arr[i], minSubarr); if (sum == currentMax) { if (cnt == 1) { minVal = min(minVal, 0); } else { minVal = min(minVal, minSubarr); } } if (currentMax < 0) { currentMax = 0; cnt = 0; minSubarr = INT_MAX; } } return sum - minVal; } int main(){ int arr[] = {1, 2, 3, -2, 3}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Maximum sum = " << getMaxSum(arr, n) << endl; return 0; }
Output
When you compile and execute the above program. It generates the following output−
Maximum sum = 9
- Related Articles
- Maximum sum subarray removing at most one element in C++
- Maximum Subarray Sum after inverting at most two elements in C++
- Maximize the subarray sum after multiplying all elements of any subarray with X in C++
- Maximum subarray sum by flipping signs of at most K array elements in C++
- Maximum Subarray Sum with One Deletion in C++
- Maximum subarray sum in an array created after repeated concatenation in C++
- Maximum subarray sum in an array created after repeated concatenation in C++ Program
- Program to find out the sum of the maximum subarray after a operation in Python
- Maximum Sum Circular Subarray in C++
- Maximum sum bitonic subarray in C++
- Finding the maximum number using at most one swap in JavaScript
- Maximum subarray sum after dividing array into subarrays based on the given queries in Java
- Maximum average of a subarray of size of at least X and at most Y in C++
- Maximize array sum after K negation in C++
- Removing smallest subarray to make array sum divisible in JavaScript

Advertisements