You are given an integer array nums. You must perform exactly one operation where you can replace one element nums[i] with nums[i] * nums[i].
Return the maximum possible subarray sum after exactly one operation. The subarray must be non-empty.
Example 1:
Input: nums = [2,-1,-4,-3] Output: 17 Explanation: You can perform the operation on index 2 (0-indexed) to make nums = [2,-1,16,-3]. Now, the maximum subarray sum is 2 + -1 + 16 = 17.
Example 2:
Input: nums = [1,-1,1,1,-1,-1,1] Output: 4 Explanation: You can perform the operation on index 1 (0-indexed) to make nums = [1,1,1,1,-1,-1,1]. Now, the maximum subarray sum is 1 + 1 + 1 + 1 = 4.
Constraints:
1 <= nums.length <= 105-104 <= nums[i] <= 104Problem Overview: You are given an integer array. You may choose exactly one element and square it (x → x²). After performing this operation, find the maximum possible sum of a contiguous subarray. The operation can significantly increase a value, so the optimal subarray may include the squared element or even start at it.
Approach 1: Brute Force with Modified Subarray Enumeration (O(n²) time, O(1) space)
Try every index as the element where the operation is applied. For each candidate index i, treat nums[i] as squared and compute the maximum subarray sum using a standard scan. This can be done by recalculating subarray sums around the modified array or running a variant of Kadane’s algorithm after applying the change. Since the operation position can be any of the n indices and each evaluation requires a full pass, the complexity becomes O(n²). This approach helps reason about how the squared element affects the surrounding subarray but does not scale well for large inputs.
Approach 2: Dynamic Programming with Two States (Kadane Variant) (O(n) time, O(1) space)
The optimal solution extends Kadane’s algorithm from Array problems with an additional state representing whether the operation has already been used. Track two running values while iterating through the array:
noOp — maximum subarray sum ending at the current index with no operation used yet.
withOp — maximum subarray sum ending at the current index where one element has already been squared.
For each number x, update the states:
noOp = max(x, noOp + x)
withOp = max(x*x, noOp_prev + x*x, withOp + x)
This transition captures all possibilities: starting a new subarray using the squared value, extending a previous subarray and applying the square at the current index, or extending a subarray where the operation was already used earlier. The answer is the maximum value seen in withOp. The algorithm processes each element once and stores only two variables, giving O(n) time and O(1) space.
This pattern is common in Dynamic Programming problems where you track states representing whether a special action has been used. Conceptually, it’s a modified Kadane’s algorithm applied to the array while maintaining two parallel subarray states.
Recommended for interviews: The dynamic programming approach with two states is the expected solution. Mentioning the brute-force idea shows you understand the effect of squaring different elements, but the O(n) Kadane-style DP demonstrates algorithmic optimization and strong familiarity with dynamic programming patterns used in array subarray problems.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Enumeration | O(n²) | O(1) | Useful for understanding the effect of squaring each index and validating logic during early problem exploration. |
| Dynamic Programming (Kadane Variant) | O(n) | O(1) | Optimal solution for interviews and production. Handles large arrays efficiently with constant memory. |
1746. Maximum Subarray Sum After One Operation - Week 5/5 Leetcode December Challenge • Programming Live with Larry • 380 views views
Watch 6 more video solutions →Practice Maximum Subarray Sum After One Operation with our built-in code editor and test cases.
Practice on FleetCode