1045. Maximum sum
Jax is trying to solve a problem but he has to go out to play with friends. So he ask you to solve the problem and he went to play with his friends.
You are given an array and you have to maximize the sum of the array by using below two operations each once.
1) You can choose any prefix of the array (possibly empty) and multiply with -1.
2) You can choose any suffix of the array (possibly empty) and multiply with -1.
The chosen prefix and suffix may intersect. You have to find the maximum sum of the array by performing the above operations.
Example 1:
Input : nums = [-1, 2 , -3]
Output : 6
Explanation :
In first operation we will choose the prefix array as [-1] and multiply it with -1.
In second operation we will choose the suffix array as [-3] and multiply it with -1.
The new array after the operations performed will be [1, 2, 3].
The maximum sum would be => 1 + 2 + 3 => 6.
Example 2:
Input : nums = [-1, 10, -5, 10, -2]
Output : 18
Explanation :
The prefix chosen is [-1].
The suffix chosen is [-2].
The new array will be -> [1, 10, -5, 10, 2].
Sum would be => 1 + 10 + (-5) + 10 + 2 => 18.
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= n <= 105
- 10-4 <= nums[i] <= 10-4