Given a set of numbers and a number k, find the maximum sum such that if you pick a number at index i you should not pick any number from index i - K to index i + K.
This problem was asked in google to my friend. I am not able to figure out a solution better then a naive O(n^2).
You can do this in O(n) by keeping track of the maximum of all values seen in the first i-K-1 entries in the array.
Python code:
For each index i we combine A[i] with m which is the highest value seen in the initial values of the array A[0],..,A[i-K-1].