#C14475. Minimum Difference in k-Subset

    ID: 44128 Type: Default 1000ms 256MiB

Minimum Difference in k-Subset

Minimum Difference in k-Subset

Given an array (A) of (n) integers and an integer (k) (where (1 \leq k \leq n)), your task is to choose any (k) elements from (A) such that the difference between the maximum and minimum elements among the chosen ones is minimized. Formally, if (S) is a subset of size (k) and (\Delta = \max(S) - \min(S)), you need to find the minimum possible (\Delta) over all possible subsets of size (k).


Example: For (A = [9, 4, 1, 7]) and (k = 2), one optimal selection is ([7, 9]) giving (\Delta = 2), which is the minimum achievable difference.

inputFormat

The input is given via standard input (stdin) and consists of two lines:

  1. The first line contains two space-separated integers (n) and (k), where (n) is the number of elements in the array, and (k) is the number of elements to choose.
  2. The second line contains (n) space-separated integers representing the elements of the array.

outputFormat

The output should be printed to standard output (stdout) and consist of a single integer: the minimum possible difference (\Delta) between the maximum and minimum of any chosen subset of (k) elements.## sample

4 2
9 4 1 7
2

</p>