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.
Example 1:
Input: arr = [1,-2,0,3] Output: 4 Explanation: Because we can choose [1, -2, 0, 3] and drop -2, thus the subarray [1, 0, 3] becomes the maximum value.
Example 2:
Input: arr = [1,-2,-2,3] Output: 3 Explanation: We just choose [3] and it's the maximum sum.
Example 3:
Input: arr = [-1,-1,-1,-1] Output: -1 Explanation: The final subarray needs to be non-empty. You can't choose [-1] and delete -1 from it, then get an empty subarray to make the sum equals to 0.
Constraints:
1 <= arr.length <= 105
-104 <= arr[i] <= 104
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We want to find the biggest possible total from a sequence of numbers, allowing ourselves to remove just one number. The brute force way is to check every single possibility by removing numbers one at a time and calculating the total.
Here's how the algorithm would work step-by-step:
def max_subarray_sum_with_one_deletion_brute_force(numbers):
maximum_sum = sum(numbers)
for index_to_remove in range(len(numbers)):
# Create a new list with the element removed
temporary_list = numbers[:index_to_remove] + numbers[index_to_remove+1:]
# Calculate the sum of the subarray after deletion.
current_sum = sum(temporary_list)
# Update the maximum sum if the current sum is greater.
maximum_sum = max(maximum_sum, current_sum)
return maximum_sum
The best strategy focuses on calculating two kinds of running sums as we go through the numbers. One sum keeps track of the largest subarray sum we've seen so far without deleting anything. The other keeps track of the largest subarray sum we've seen so far *with* a deletion.
Here's how the algorithm would work step-by-step:
def maximum_subarray_sum_with_one_deletion(numbers):
max_sum_without_deletion = float('-inf')
current_max_sum = 0
for number in numbers:
current_max_sum = max(number, current_max_sum + number)
max_sum_without_deletion = max(max_sum_without_deletion, current_max_sum)
max_sum_ending_here = [0] * len(numbers)
current_max_sum = 0
for index, number in enumerate(numbers):
current_max_sum = max(number, current_max_sum + number)
max_sum_ending_here[index] = current_max_sum
max_sum_starting_here = [0] * len(numbers)
current_max_sum = 0
for index in range(len(numbers) - 1, -1, -1):
number = numbers[index]
current_max_sum = max(number, current_max_sum + number)
max_sum_starting_here[index] = current_max_sum
max_sum_with_deletion = float('-inf')
# Iterate and simulate deletion at each index
for index in range(len(numbers)):
if index == 0:
max_sum_with_deletion = max(max_sum_with_deletion, max_sum_starting_here[1])
elif index == len(numbers) - 1:
max_sum_with_deletion = max(max_sum_with_deletion, max_sum_ending_here[index - 1])
else:
max_sum_with_deletion = max(max_sum_with_deletion, max_sum_ending_here[index - 1] + max_sum_starting_here[index + 1])
# Crucial: Compare sums to find the absolute maximum.
return max(max_sum_without_deletion, max_sum_with_deletion)
Case | How to Handle |
---|---|
Null or empty input array | Return 0 as there's no subarray to sum. |
Array with a single element | Return the single element's value as deleting it results in 0, which is always less than the original value. |
Array with all negative numbers | Return the largest (least negative) number in the array, which is the maximum subarray after deleting all other numbers. |
Array with all zeros | Return the sum of the entire array which is 0. |
Array with positive and negative numbers such that the overall sum is negative, but a subarray sum is positive. | Kadane's algorithm (with and without deletion consideration) will handle finding the maximum subarray sum even when the overall sum is negative. |
Integer overflow potential when summing large numbers. | Use a larger data type (e.g., long) to store the sums to prevent overflow. |
Array contains MAX_INT and large negative numbers which could cause overflow after deletion. | Long data type prevents intermediate value overflow. |
A very large input array. | The solution's time complexity should be O(n) to efficiently handle large arrays using dynamic programming. |