Two players A and B are playing a game with array A of N elements in which each player chooses an array interval with maximum sum.The other simultaneously chooses another interval in the same array such that both the chosen intervals follow certain rules :
Two chosen intervals shouldn't have anything between them . I.e. they should not be overlapping.
Two intervals chosen shouldn't be too close to each other. There must be at least K index distance between the finish of the first interval and the start of the second interval.
The sum of the values in chosen intervals should be as big as possible. Or we can say both chosen intervals sum should be as large as possible.
I want sum of these two intervals.
Example :
Let N equals to 8, K equals to 3, A[] equals to {6, 6, 0, -1, 4, 0, 3, -1}. It is optimal to choose [1, 2] and [7, 7] intervals. That is the only optimal choice that we can make.
And total sum if 12(from first interval) + 6(from second interval) = 18
I know about kadane algorithm for finding maximum sum continuous subarray .But getting trouble to implement for this problem.