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] <= 104When 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:
This problem is solved using dynamic programming similar to Kadane's algorithm. We maintain two running states representing the maximum subarray sum ending at the current element: one where no operation has been used yet, and one where exactly one element has been squared. At each step, we update both states and keep track of the overall maximum sum achieved using the operation.
Here's how the algorithm would work step-by-step:
def max_sum_after_operation(nums: list[int]) -> int:
# Initialize state tracking without and with the squared operation
max_without_op = 0
max_with_op = 0
overall_max = -float('inf')
for value in nums:
squared_value = value * value
# Update the maximum subarray sum ending here that includes one squared element
max_with_op = max(squared_value, max_without_op + squared_value, max_with_op + value)
# Update the maximum subarray sum ending here without any operation applied
max_without_op = max(value, max_without_op + value)
# Track the global maximum among all valid subarrays with one squared element
if max_with_op > overall_max:
overall_max = max_with_op
return overall_max
To find the maximum possible score by changing just one number, we can look at every part of the sequence and keep track of two different scenarios. The smart shortcut involves deciding whether we have already used our one special power to double a number, or if we are saving it for later.
Here's how the algorithm would work step-by-step:
def max_subarray_sum_after_one_operation(number_sequence):
# Initialize tracking variables for standard and modified maximum sums at the very first element.
current_standard_sum = number_sequence[0]
current_modified_sum = number_sequence[0] * number_sequence[0]
maximum_score_found = current_modified_sum
# Iterate through the remaining numbers to evaluate streak extensions and power usage choices.
for current_number in number_sequence[1:]:
# Decide whether to extend the previous standard streak or start a completely new one.
next_standard_sum = max(current_number, current_standard_sum + current_number)
# Choose whether to apply the squared boost right now or inherit a previously boosted streak.
next_modified_sum = max(current_number * current_number, current_standard_sum + current_number * current_number, current_modified_sum + current_number)
current_standard_sum = next_standard_sum
current_modified_sum = next_modified_sum
maximum_score_found = max(maximum_score_found, current_standard_sum, current_modified_sum)
return maximum_score_found
| Case | How to Handle |
|---|---|
| Array with a single element | The algorithm must return the square of that single element since squaring is optional and maximizes the single-element subarray. |
| Array containing all negative numbers | Squaring the largest negative number (closest to zero) will yield a positive value, which should then dominate the maximum subarray sum. |
| Array with all zeros | The maximum subarray sum will be zero, as squaring zero yields zero and avoids negative contributions. |
| Very large numbers near the integer limits | Squaring large integers can cause integer overflow, requiring the use of 64-bit integer types throughout the calculations. |
| Array where no operation should be used | The dynamic programming state must track both the maximum sum with the squared operation used and without it, allowing the standard Kadane's result to be optimal. |
| Massive input array up to the maximum constraints | An efficient O(n) single-pass dynamic programming approach ensures the solution runs well within time limits without exceeding memory. |
| Alternating positive and negative numbers | The state transitions must correctly decide whether to square a negative number to bridge positive subarrays or leave it alone. |