Google Find the min amplitude after removing K consecutive elements in an array.

you are given an array A of N positive integers and an integer k. You want to remove k consecutive elements from A such that the amplitude of remaining element is minimal. Amplitude is the the difference between the minimal and maximal elements.

For eg. A[] = {8,7,4,1) and k=2. Output should be 1 becuase we will remove 4 and 1.

If we use brute force, that will be N^2. Any suggestion to improve the efficency.

I can sense that we should slide windows to resolve the issue with O(n), b ut not sure how.

Comments (6)