cswork / OA打开练习工作台

AmazonOA #167

Find Min Max Difference

估计中等可在线判题

考点(AI 标注)双指针滑窗单调栈与堆原地数组

Keep n−k of the original elements, sort the kept elements, and take their maximum adjacent difference; minimize that value. When only 1 element is kept, this site's convention is that the maximum adjacent difference is 0. The source text is truncated; the objective of this problem was recovered from its complete example explanation, and it is not to minimize the maximum minus the minimum of the whole range.

输入格式

The first line contains n k, and the second line contains n numbers. This site sets 1≤n≤200000, 0≤k<n, and values in −10^9..10^9.

输出格式

Output a single integer answer.

样例 1

输入:

5 2
1 4 3 6 5

输出:

1

样例 2

输入:

3 2
1 10 20

输出:

0

样例 3

输入:

4 0
1 4 8 9

输出:

4

样例说明

Example 1: keep 3, 4, 5, for a maximum adjacent difference of 1. Example 2: only one element is kept, so the answer is 0. Example 3: no number can be deleted, the adjacent differences are 3, 4, 1, so the answer is 4.