#K73642. Minimum Difference in Subset

    ID: 34021 Type: Default 1000ms 256MiB

Minimum Difference in Subset

Minimum Difference in Subset

You are given a collection of N books, each with a coolness score. Your task is to select exactly M books such that the difference between the maximum and minimum coolness scores in this subset is minimized.

Problem Details:

  • You are provided with an integer N representing the total number of books.
  • An integer M is also provided which represents the number of books you must select.
  • A list of N integers represents the coolness scores of the books.

Your goal is to choose M books such that if you let \( S \) be the chosen subset, the value \[ \min_{S, |S|=M} \left( \max(S) - \min(S) \right) \] is as small as possible.

Hint: Sorting the list of scores may help find the optimal subset by considering consecutive segments.

inputFormat

The first line of input contains two space-separated integers, N and M.

The second line contains N space-separated integers representing the coolness scores of the books.

outputFormat

Output a single integer which is the minimum possible difference between the maximum and minimum coolness scores among the selected M books.

## sample
5 3
10 3 20 7 15
7