Maximum Charge After Removal
Select a system and remove it, causing the neighboring systems to automatically merge and combine their charge values.
If the removed system has neighboring systems with charges x and y directly to its left and right, they will combine to form a new system with charge x + y. No combination will take place if the system is the leftmost or rightmost in the array.
Since this process is computationally expensive, the engineers will simulate the operation using Amazon's advanced tools.
For example, if the system charges are [-3, 1, 4, -1, 5, -9], using the tool on the 4th system (index 3) will result in the new sequence [-3, 1, 9, -9], as the charges from the 3rd and 5th systems combine to 4 + 5 = 9. If they then use the tool on the 1st system in this new sequence, it will become [1, 9, -9].
The engineers will continue performing these operations until only one system remains.
Given an array charge of size n, find the maximum possible charge of the remaining system after performing these operations.
Complete the function maximumChargeAfterRemoval in the editor.
maximumChargeAfterRemoval has the following parameter:
int[] charge: an array of integers representing the charges
Returns
int: the maximum possible charge of the remaining system
1Example 1
One way the engineers can remove the system charges is as follows:
Index of the charge removed(0-indexed) New state of charges
[4, 3, -2, 1] -> [4, 4]
[4, 4] -> [4]
The maximum possible charge of the remaining system is 4.
Constraints
Limits and guarantees your solution can rely on.
🐱🐱