Daily Coding Challenge
Solve today's challenge, earn streaks, and climb the leaderboard!
Max Subarray One Deletion
Find the maximum sum of a non-empty subarray, potentially after deleting at most one element from it. This means you can either take a regular subarray sum or take a subarray sum after removing one element within it.
Problem Statement
Given an array of integers `arr`, return the maximum possible sum of a non-empty subarray. You are allowed to delete at most one element from the chosen subarray. The subarray must remain non-empty after deletion (if any). For example, if you select the subarray `[5]` and delete `5`, the resulting subarray is empty, which is an invalid operation. However, if you choose `[5, -1, 3]` and delete `-1`, the sum is `5 + 3 = 8`. If you choose not to delete any element from `[5, -1, 3]`, the sum is `5 + (-1) + 3 = 7`. Your solution should determine the absolute maximum sum possible under these conditions. The constraints on `arr` are `1 <= arr.length <= 10^5` and `-10^4 <= arr[i] <= 10^4`.
Example Test Cases
arr = [1, -2, 0, 3]
4
Deleting -2 from the subarray [1, -2, 0, 3] yields [1, 0, 3] with sum 4. This is the maximum possible sum.
arr = [1, 2, 3]
6
No deletion is needed. The subarray [1, 2, 3] has sum 6, which is the maximum.
Sign in to save your progress and compete on the leaderboard
Leaderboard
Max Subarray One Deletion
No submissions yet for this filter
Be the first to solve this challenge!