UberOA #14
Mountain Peaks — Min Difference With viewingGap
Estimated MediumJudge-ready
Topics (AI-labelled)Binary SearchIn-place Arrays
Choose a pair of peaks whose index distance is at least viewingGap, minimizing the absolute difference of their heights. A distance equal to viewingGap is allowed; when gap=1, any two distinct indices are allowed, so the wording "non-adjacent" in the original second example is inaccurate.
This site's standard input/output is stated explicitly.
Input
The first line contains n viewingGap, and the second line contains n heights. 2≤n≤100000, 1≤viewingGap<n, 1≤height[i]≤10⁹.
Output
Output one integer answer.
Example 1
Input:
5 3
1 5 4 10 9
Output:
4
Example 2
Input:
4 1
3 10 5 8
Output:
2
Example 3
Input:
3 2
7 2 7
Output:
0
Example notes
Example 1: the three allowed pairs have height differences 9, 8, and 4, so the minimum is 4. Example 2: 3 and 5, as well as 10 and 8, both differ by 2 with an index distance of at least 1, so the answer is 2. Example 3: the first and last positions are 2 apart and have the same height, so the answer is 0.
Editorial
Loading solution…