Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Think of dynamic programming.
Another approach would be a two-pointer.
Start from the first index and traverse until the last one.
At each step, store the sum of the suffix that forms a consecutive subarray.
Do the above for both +1 and -1 and add them up.