my problem is that I'm given an array of with length l.
let's say this is my array: [1,5,4,2,9,3,6]
let's call this A
.
This array can have multiple sub arrays with nodes being adjacent to each other. so we can have [1,5,4]
or [2,9,3,6]
and so on. the length of each sub array does not matter.
But the trick is the sum part. we cannot just add all numbers, it works like flip flop. so for the sublist [2,9,3,6]
the sum would be [2,-9,3,-6]
which is: -10
. and is pretty small.
what would be the sublist (or sub-array if you like) of this array A that produces the maximum sum?
one possible way would be (from intuition) that the sublist [4,2,9]
will output a decent result : [4, -2, 9]
= (add all the elements) = 11
.
The question is, how to come up with a result like this? what is the sub-array that gives us the maximum flip-flop sum?
and mainly, what is the algorithm that takes any array as an input and outputs a sub-array with all numbers being adjacent and with the maximum sum?
I haven't come up with anything but I'm pretty sure I should pick either dynamic programming or divide and conquer to solve this issue. again, I don't know, I may be totally wrong.