#K10496. Minimum Maximum Difference in Subset

    ID: 23259 Type: Default 1000ms 256MiB

Minimum Maximum Difference in Subset

Minimum Maximum Difference in Subset

Given an array of N integers and a positive integer K, the task is to select K elements from the array such that the difference between the maximum and minimum elements among the selected ones is minimized. In other words, if you choose a subset S with exactly K elements, you need to minimize (\max(S) - \min(S)).

The input format provides the number N, the value K, and then N integers. Your program should output the minimum possible difference. It is guaranteed that 1 (\le) K (\le) N.

inputFormat

The first line contains two integers N and K, where N is the number of elements in the array and K is the number of elements to choose. The second line contains N space-separated integers.

outputFormat

Output a single integer which is the minimum possible difference between the maximum and minimum of the selected K elements.## sample

6 3
1 3 6 4 1 2
1