
- 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
Maximum Subarray Sum with One Deletion in C++
Suppose we have an array of integers; we have to find the maximum sum for a non-empty subarray (contiguous elements) with at most one element deletion. In other words, we can say that we want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the sum of the remaining elements is maximum possible. We have to keep in mind that the subarray needs to be non-empty after deleting one element. So if the input is like [1,-2,0,3], then the output will be 4. So if we delete -2, it will return the max sum.
To solve this, we will follow these steps −
- n := size of array, and := a[0]
- suff_with_del := 0, suff_with_out_del := a[0]
- for i in range i to n – 1
- suff_with_del := max of suff_with_del + a[i] and suff_with_out_del
- suff_with_out_del := max of a[i] and suff_with_out_del + a[i]
- ans := max of ans, suff_with_out_del and suff_with _del
- return res
Example(C++)
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: int maximumSum(vector<int>& a) { int n = a.size(); int ans = a[0]; int suffix_with_deletion = 0; int suffix_with_not_deletion = a[0]; for(int i = 1;i<n;i++){ suffix_with_deletion = max(suffix_with_deletion + a[i], suffix_with_not_deletion); suffix_with_not_deletion = max(a[i],suffix_with_not_deletion+a[i]); ans = max({ans, suffix_with_not_deletion,suffix_with_deletion}); } return ans; } }; main(){ vector<int> v = {1,-2,0,3}; Solution ob; cout <<ob.maximumSum(v); }
Input
[1,-2,0,3]
Output
4
- Related Articles
- Maximum sum subarray removing at most one element in C++
- Maximum Sum Circular Subarray in C++
- Maximum sum bitonic subarray in C++
- Maximum subarray sum modulo m in C++
- Maximum circular subarray sum in C++\n
- Maximum contiguous sum of subarray in JavaScript
- Maximize the maximum subarray sum after removing at most one element in C++
- Maximum Size Subarray Sum Equals k in C++
- Find Maximum Sum Strictly Increasing Subarray in C++
- Maximum Subarray Sum Excluding Certain Elements in C++
- Maximum subarray sum in circular array using JavaScript
- Maximum subarray sum in O(n) using prefix sum in C++
- Maximum Subarray Sum in a given Range in C++
- Maximum Sum SubArray using Divide and Conquer in C++
- Maximum Subarray Sum Excluding Certain Elements in C++ program

Advertisements