#K55447. Minimum Difference Subset
Minimum Difference Subset
Minimum Difference Subset
You are given an array of n integers and an integer K. Your task is to select any K elements from the array such that the difference between the maximum and minimum of these selected elements is minimized.
In other words, if after selecting and then sorting the K elements you obtain an array a, you need to minimize the value
$$\min_{0 \le i \le n-K} (a[i+K-1]-a[i])$$
Note: The chosen elements do not need to be consecutive in the original array. You can choose any K elements.
Example:
Input: 6 3 1 3 6 4 1 2 After selecting and sorting a possible subset [1,1,2], the difference is 2-1 = 1. Output: 1
inputFormat
The first line contains two integers n and K separated by a space, where n is the number of elements in the array. The second line contains n integers separated by spaces representing the array.
\(1 \leq K \leq n\)
outputFormat
Output a single integer: the minimum absolute difference between the maximum and minimum among any chosen subset of K elements.
## sample6 3
1 3 6 4 1 2
1
</p>