I've been trying to solve this problem:
https://leetcode.com/problems/maximum-subarray-sum-with-one-deletion/description/
Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elements) with at most one element deletion. In other words, you 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.
Note that the subarray needs to be non-empty after deleting one element."
but I'm unable to think how to approach this problem.. :| any help anyone please?! I tried understanding from LLMs videos but failed to..









Are you familiar with the standard Kadane's max subarray sum?
Then how can you apply that kind of idea to this problem?
Max subarray sum that ends at this element but we haven't deleted an element yet (so we can delete an element later)
Max subarray sum with deleted element that ends at this element
The problem requires you to take a :
1) Consecutive non-empty subarray.
2) Or a subarray that has an element missed in the middle of it.
For the first case, see Kadane's Algorithm explanation here :
https://www.geeksforgeeks.org/dsa/largest-sum-contiguous-subarray/
Now let's see the second case where we should find a consecutive subarray from L to R.. but there is an index i such that L < i < R missed, let's iterate over all possible 'i' that may be missed .. then we have to find some L and R such that the sum of two segments (L->i-1), (i+1->R) is maximized.. this can be done using prefix sums.
I've implemented the solution and got accepted :