Below is the implementation of the maximum subarray finder:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134/** * Find the maximum sum of a subarray with at most one element deletion. * Dynamic Programming approach. * * @param {number[]} arr - The input array * @return {number} - The maximum sum of a subarray with at most one element deletion */function maximumSum(arr) { const n = arr.length; // Handle edge case if (n === 1) { return arr[0]; } // Initialize arrays const maxEndHere = new Array(n); const maxEndHereWithDeletion = new Array(n); maxEndHere[0] = arr[0]; maxEndHereWithDeletion[0] = 0; let maxSoFar = arr[0]; for (let i = 1; i < n; i++) { // Update maximum subarray sum ending at position i without deletion maxEndHere[i] = Math.max(arr[i], maxEndHere[i - 1] + arr[i]); // Update maximum subarray sum ending at position i with one deletion maxEndHereWithDeletion[i] = Math.max( maxEndHereWithDeletion[i - 1] + arr[i], maxEndHere[i - 1] ); // Update maximum subarray sum so far maxSoFar = Math.max(maxSoFar, maxEndHere[i], maxEndHereWithDeletion[i]); } return maxSoFar;} /** * Find the maximum sum of a subarray with at most one element deletion. * Space-Optimized Dynamic Programming approach. * * @param {number[]} arr - The input array * @return {number} - The maximum sum of a subarray with at most one element deletion */function maximumSumOptimized(arr) { const n = arr.length; // Handle edge case if (n === 1) { return arr[0]; } // Initialize variables let maxEndHere = arr[0]; let maxEndHereWithDeletion = 0; let maxSoFar = arr[0]; for (let i = 1; i < n; i++) { // Update maximum subarray sum ending at position i with one deletion maxEndHereWithDeletion = Math.max( maxEndHereWithDeletion + arr[i], maxEndHere ); // Update maximum subarray sum ending at position i without deletion maxEndHere = Math.max(arr[i], maxEndHere + arr[i]); // Update maximum subarray sum so far maxSoFar = Math.max(maxSoFar, maxEndHere, maxEndHereWithDeletion); } return maxSoFar;} /** * Find the maximum sum of a subarray with at most one element deletion. * Prefix and Suffix Sum approach. * * @param {number[]} arr - The input array * @return {number} - The maximum sum of a subarray with at most one element deletion */function maximumSumPrefixSuffix(arr) { const n = arr.length; // Handle edge case if (n === 1) { return arr[0]; } // Compute prefix sums (maximum subarray sum ending at each position) const prefix = new Array(n); prefix[0] = arr[0]; for (let i = 1; i < n; i++) { prefix[i] = Math.max(arr[i], prefix[i - 1] + arr[i]); } // Compute suffix sums (maximum subarray sum starting from each position) const suffix = new Array(n); suffix[n - 1] = arr[n - 1]; for (let i = n - 2; i >= 0; i--) { suffix[i] = Math.max(arr[i], suffix[i + 1] + arr[i]); } // Find maximum subarray sum without deletion let maxWithoutDeletion = Math.max(...prefix); // Find maximum subarray sum with one deletion let maxWithDeletion = -Infinity; for (let i = 1; i < n - 1; i++) { maxWithDeletion = Math.max(maxWithDeletion, prefix[i - 1] + suffix[i + 1]); } // Return the maximum of the two return Math.max(maxWithoutDeletion, maxWithDeletion);} // Test casesconsole.log(maximumSum([1, -2, 0, 3])); // 4console.log(maximumSum([1, -2, -2, 3])); // 3console.log(maximumSum([-1, -1, -1, -1])); // -1 console.log(maximumSumOptimized([1, -2, 0, 3])); // 4console.log(maximumSumOptimized([1, -2, -2, 3])); // 3console.log(maximumSumOptimized([-1, -1, -1, -1])); // -1 console.log(maximumSumPrefixSuffix([1, -2, 0, 3])); // 4console.log(maximumSumPrefixSuffix([1, -2, -2, 3])); // 3console.log(maximumSumPrefixSuffix([-1, -1, -1, -1])); // -1
Let's break down the implementation:
Implement the maximum subarray finder solution in different programming languages.
Below is the implementation of the maximum subarray finder in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134/** * Find the maximum sum of a subarray with at most one element deletion. * Dynamic Programming approach. * * @param {number[]} arr - The input array * @return {number} - The maximum sum of a subarray with at most one element deletion */function maximumSum(arr) { const n = arr.length; // Handle edge case if (n === 1) { return arr[0]; } // Initialize arrays const maxEndHere = new Array(n); const maxEndHereWithDeletion = new Array(n); maxEndHere[0] = arr[0]; maxEndHereWithDeletion[0] = 0; let maxSoFar = arr[0]; for (let i = 1; i < n; i++) { // Update maximum subarray sum ending at position i without deletion maxEndHere[i] = Math.max(arr[i], maxEndHere[i - 1] + arr[i]); // Update maximum subarray sum ending at position i with one deletion maxEndHereWithDeletion[i] = Math.max( maxEndHereWithDeletion[i - 1] + arr[i], maxEndHere[i - 1] ); // Update maximum subarray sum so far maxSoFar = Math.max(maxSoFar, maxEndHere[i], maxEndHereWithDeletion[i]); } return maxSoFar;} /** * Find the maximum sum of a subarray with at most one element deletion. * Space-Optimized Dynamic Programming approach. * * @param {number[]} arr - The input array * @return {number} - The maximum sum of a subarray with at most one element deletion */function maximumSumOptimized(arr) { const n = arr.length; // Handle edge case if (n === 1) { return arr[0]; } // Initialize variables let maxEndHere = arr[0]; let maxEndHereWithDeletion = 0; let maxSoFar = arr[0]; for (let i = 1; i < n; i++) { // Update maximum subarray sum ending at position i with one deletion maxEndHereWithDeletion = Math.max( maxEndHereWithDeletion + arr[i], maxEndHere ); // Update maximum subarray sum ending at position i without deletion maxEndHere = Math.max(arr[i], maxEndHere + arr[i]); // Update maximum subarray sum so far maxSoFar = Math.max(maxSoFar, maxEndHere, maxEndHereWithDeletion); } return maxSoFar;} /** * Find the maximum sum of a subarray with at most one element deletion. * Prefix and Suffix Sum approach. * * @param {number[]} arr - The input array * @return {number} - The maximum sum of a subarray with at most one element deletion */function maximumSumPrefixSuffix(arr) { const n = arr.length; // Handle edge case if (n === 1) { return arr[0]; } // Compute prefix sums (maximum subarray sum ending at each position) const prefix = new Array(n); prefix[0] = arr[0]; for (let i = 1; i < n; i++) { prefix[i] = Math.max(arr[i], prefix[i - 1] + arr[i]); } // Compute suffix sums (maximum subarray sum starting from each position) const suffix = new Array(n); suffix[n - 1] = arr[n - 1]; for (let i = n - 2; i >= 0; i--) { suffix[i] = Math.max(arr[i], suffix[i + 1] + arr[i]); } // Find maximum subarray sum without deletion let maxWithoutDeletion = Math.max(...prefix); // Find maximum subarray sum with one deletion let maxWithDeletion = -Infinity; for (let i = 1; i < n - 1; i++) { maxWithDeletion = Math.max(maxWithDeletion, prefix[i - 1] + suffix[i + 1]); } // Return the maximum of the two return Math.max(maxWithoutDeletion, maxWithDeletion);} // Test casesconsole.log(maximumSum([1, -2, 0, 3])); // 4console.log(maximumSum([1, -2, -2, 3])); // 3console.log(maximumSum([-1, -1, -1, -1])); // -1 console.log(maximumSumOptimized([1, -2, 0, 3])); // 4console.log(maximumSumOptimized([1, -2, -2, 3])); // 3console.log(maximumSumOptimized([-1, -1, -1, -1])); // -1 console.log(maximumSumPrefixSuffix([1, -2, 0, 3])); // 4console.log(maximumSumPrefixSuffix([1, -2, -2, 3])); // 3console.log(maximumSumPrefixSuffix([-1, -1, -1, -1])); // -1
First, understand that we need to find the maximum sum of a subarray after removing at most one element from the array.
Recognize that this is an extension of the classic maximum subarray problem (Kadane's algorithm), but with the additional option of deleting one element.
Define two states: the maximum subarray sum ending at each position without any deletion, and the maximum subarray sum ending at each position with one deletion.
Implement the dynamic programming solution by updating the two states for each position in the array.
Optimize the space complexity by observing that we only need the values from the previous position to compute the current values.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the maximum subarray finder problem using a different approach than shown above.
Handle the case where the array has only one element (return the element itself).
Handle the case where all elements are negative (return the maximum element).
Handle the case where the maximum subarray sum is achieved without deleting any element.
Below is the implementation of the maximum subarray finder:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134/** * Find the maximum sum of a subarray with at most one element deletion. * Dynamic Programming approach. * * @param {number[]} arr - The input array * @return {number} - The maximum sum of a subarray with at most one element deletion */function maximumSum(arr) { const n = arr.length; // Handle edge case if (n === 1) { return arr[0]; } // Initialize arrays const maxEndHere = new Array(n); const maxEndHereWithDeletion = new Array(n); maxEndHere[0] = arr[0]; maxEndHereWithDeletion[0] = 0; let maxSoFar = arr[0]; for (let i = 1; i < n; i++) { // Update maximum subarray sum ending at position i without deletion maxEndHere[i] = Math.max(arr[i], maxEndHere[i - 1] + arr[i]); // Update maximum subarray sum ending at position i with one deletion maxEndHereWithDeletion[i] = Math.max( maxEndHereWithDeletion[i - 1] + arr[i], maxEndHere[i - 1] ); // Update maximum subarray sum so far maxSoFar = Math.max(maxSoFar, maxEndHere[i], maxEndHereWithDeletion[i]); } return maxSoFar;} /** * Find the maximum sum of a subarray with at most one element deletion. * Space-Optimized Dynamic Programming approach. * * @param {number[]} arr - The input array * @return {number} - The maximum sum of a subarray with at most one element deletion */function maximumSumOptimized(arr) { const n = arr.length; // Handle edge case if (n === 1) { return arr[0]; } // Initialize variables let maxEndHere = arr[0]; let maxEndHereWithDeletion = 0; let maxSoFar = arr[0]; for (let i = 1; i < n; i++) { // Update maximum subarray sum ending at position i with one deletion maxEndHereWithDeletion = Math.max( maxEndHereWithDeletion + arr[i], maxEndHere ); // Update maximum subarray sum ending at position i without deletion maxEndHere = Math.max(arr[i], maxEndHere + arr[i]); // Update maximum subarray sum so far maxSoFar = Math.max(maxSoFar, maxEndHere, maxEndHereWithDeletion); } return maxSoFar;} /** * Find the maximum sum of a subarray with at most one element deletion. * Prefix and Suffix Sum approach. * * @param {number[]} arr - The input array * @return {number} - The maximum sum of a subarray with at most one element deletion */function maximumSumPrefixSuffix(arr) { const n = arr.length; // Handle edge case if (n === 1) { return arr[0]; } // Compute prefix sums (maximum subarray sum ending at each position) const prefix = new Array(n); prefix[0] = arr[0]; for (let i = 1; i < n; i++) { prefix[i] = Math.max(arr[i], prefix[i - 1] + arr[i]); } // Compute suffix sums (maximum subarray sum starting from each position) const suffix = new Array(n); suffix[n - 1] = arr[n - 1]; for (let i = n - 2; i >= 0; i--) { suffix[i] = Math.max(arr[i], suffix[i + 1] + arr[i]); } // Find maximum subarray sum without deletion let maxWithoutDeletion = Math.max(...prefix); // Find maximum subarray sum with one deletion let maxWithDeletion = -Infinity; for (let i = 1; i < n - 1; i++) { maxWithDeletion = Math.max(maxWithDeletion, prefix[i - 1] + suffix[i + 1]); } // Return the maximum of the two return Math.max(maxWithoutDeletion, maxWithDeletion);} // Test casesconsole.log(maximumSum([1, -2, 0, 3])); // 4console.log(maximumSum([1, -2, -2, 3])); // 3console.log(maximumSum([-1, -1, -1, -1])); // -1 console.log(maximumSumOptimized([1, -2, 0, 3])); // 4console.log(maximumSumOptimized([1, -2, -2, 3])); // 3console.log(maximumSumOptimized([-1, -1, -1, -1])); // -1 console.log(maximumSumPrefixSuffix([1, -2, 0, 3])); // 4console.log(maximumSumPrefixSuffix([1, -2, -2, 3])); // 3console.log(maximumSumPrefixSuffix([-1, -1, -1, -1])); // -1
Let's break down the implementation:
Implement the maximum subarray finder solution in different programming languages.
Below is the implementation of the maximum subarray finder in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134/** * Find the maximum sum of a subarray with at most one element deletion. * Dynamic Programming approach. * * @param {number[]} arr - The input array * @return {number} - The maximum sum of a subarray with at most one element deletion */function maximumSum(arr) { const n = arr.length; // Handle edge case if (n === 1) { return arr[0]; } // Initialize arrays const maxEndHere = new Array(n); const maxEndHereWithDeletion = new Array(n); maxEndHere[0] = arr[0]; maxEndHereWithDeletion[0] = 0; let maxSoFar = arr[0]; for (let i = 1; i < n; i++) { // Update maximum subarray sum ending at position i without deletion maxEndHere[i] = Math.max(arr[i], maxEndHere[i - 1] + arr[i]); // Update maximum subarray sum ending at position i with one deletion maxEndHereWithDeletion[i] = Math.max( maxEndHereWithDeletion[i - 1] + arr[i], maxEndHere[i - 1] ); // Update maximum subarray sum so far maxSoFar = Math.max(maxSoFar, maxEndHere[i], maxEndHereWithDeletion[i]); } return maxSoFar;} /** * Find the maximum sum of a subarray with at most one element deletion. * Space-Optimized Dynamic Programming approach. * * @param {number[]} arr - The input array * @return {number} - The maximum sum of a subarray with at most one element deletion */function maximumSumOptimized(arr) { const n = arr.length; // Handle edge case if (n === 1) { return arr[0]; } // Initialize variables let maxEndHere = arr[0]; let maxEndHereWithDeletion = 0; let maxSoFar = arr[0]; for (let i = 1; i < n; i++) { // Update maximum subarray sum ending at position i with one deletion maxEndHereWithDeletion = Math.max( maxEndHereWithDeletion + arr[i], maxEndHere ); // Update maximum subarray sum ending at position i without deletion maxEndHere = Math.max(arr[i], maxEndHere + arr[i]); // Update maximum subarray sum so far maxSoFar = Math.max(maxSoFar, maxEndHere, maxEndHereWithDeletion); } return maxSoFar;} /** * Find the maximum sum of a subarray with at most one element deletion. * Prefix and Suffix Sum approach. * * @param {number[]} arr - The input array * @return {number} - The maximum sum of a subarray with at most one element deletion */function maximumSumPrefixSuffix(arr) { const n = arr.length; // Handle edge case if (n === 1) { return arr[0]; } // Compute prefix sums (maximum subarray sum ending at each position) const prefix = new Array(n); prefix[0] = arr[0]; for (let i = 1; i < n; i++) { prefix[i] = Math.max(arr[i], prefix[i - 1] + arr[i]); } // Compute suffix sums (maximum subarray sum starting from each position) const suffix = new Array(n); suffix[n - 1] = arr[n - 1]; for (let i = n - 2; i >= 0; i--) { suffix[i] = Math.max(arr[i], suffix[i + 1] + arr[i]); } // Find maximum subarray sum without deletion let maxWithoutDeletion = Math.max(...prefix); // Find maximum subarray sum with one deletion let maxWithDeletion = -Infinity; for (let i = 1; i < n - 1; i++) { maxWithDeletion = Math.max(maxWithDeletion, prefix[i - 1] + suffix[i + 1]); } // Return the maximum of the two return Math.max(maxWithoutDeletion, maxWithDeletion);} // Test casesconsole.log(maximumSum([1, -2, 0, 3])); // 4console.log(maximumSum([1, -2, -2, 3])); // 3console.log(maximumSum([-1, -1, -1, -1])); // -1 console.log(maximumSumOptimized([1, -2, 0, 3])); // 4console.log(maximumSumOptimized([1, -2, -2, 3])); // 3console.log(maximumSumOptimized([-1, -1, -1, -1])); // -1 console.log(maximumSumPrefixSuffix([1, -2, 0, 3])); // 4console.log(maximumSumPrefixSuffix([1, -2, -2, 3])); // 3console.log(maximumSumPrefixSuffix([-1, -1, -1, -1])); // -1
First, understand that we need to find the maximum sum of a subarray after removing at most one element from the array.
Recognize that this is an extension of the classic maximum subarray problem (Kadane's algorithm), but with the additional option of deleting one element.
Define two states: the maximum subarray sum ending at each position without any deletion, and the maximum subarray sum ending at each position with one deletion.
Implement the dynamic programming solution by updating the two states for each position in the array.
Optimize the space complexity by observing that we only need the values from the previous position to compute the current values.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the maximum subarray finder problem using a different approach than shown above.
Handle the case where the array has only one element (return the element itself).
Handle the case where all elements are negative (return the maximum element).
Handle the case where the maximum subarray sum is achieved without deleting any element.