See the previous post for details and test cases. There I solved the problem using an intuitive but performance-wise terrible algorithm. Here I give a linear solution based on the Kadane's algorithm.

Codility itself gives us a big hint placing this problem in its Maximum slice section. The question is, how to adapt the Kadane's algorithm here, where we have an hole in the interval?

The solution requires us to change perspective. Instead of looking at the central element as an enemy, think to it as the pivot of the solution. This leads almost automatically to a clean specular solution, where we have two sub-intervals that go from one limit of the vector to the central pivot.

Looking at the code that I have written should make things clearer:

int solution(std::vector<int>& input) { if(input.size() < 4) return 0; std::vector<int> left; // 1 left.reserve(input.size() - 2); left.push_back(0); // 2 for(auto it = input.begin() + 1; it < input.end() - 2; ++it) // 3 { left.push_back(std::max(0, left.back() + *it)); } std::vector<int> right; // 4 right.reserve(input.size() - 2); right.push_back(0); for(auto it = input.rbegin() + 1; it < input.rend() - 2; ++it) { right.push_back(std::max(0, right.back() + *it)); } int result = 0; // 5 auto ir = right.rbegin(); for(auto il = left.begin(); il != left.end(); ++il, ++ir) { int tentative = *il + *ir; result = std::max(result, tentative); } return result; }1. In the left vector I am going to store all the maximum subarray values from the first interesting value up to the last one. Each value represent the best result for the left slice, up to the current pivot position.

2. If the pivot is in the first valid position, there wouldn't be anything to its left, so the first element in the left vector would be a zero.

3. All the other left elements are calculated following the Kadane's algorithm.

4. The right vector is exactly the same of the left one, just mirrored through the pivot. If the pivot was in the most rightwise position, there would be nothing in the right slice, and a zero in the right vector. Than, it is just applying Kadane moving from right to left on the input collection.

5. Now we have to combine the two partial results to get the expected solution. The vector left contains the maximum subarray values up to the pivot, the right one down to it. So, what we need to do is adding the first element in left with the last element in right, and move step by step to check all the possible solutions.

## No comments:

## Post a Comment