Problem 314
Chocolate Distribution
Easy · 10 pts · Array, Sorting, Sliding Window
Problem
There are N packets of chocolate, and packet i holds A[i] chocolates. You must give exactly one packet to each of M students. To be as fair as possible, choose the M packets so that the difference between the largest and the smallest chosen packet is as small as possible. Print that smallest possible difference.
Input
The first line contains N and M. The second line contains the N packet sizes.
Output
Print the minimum possible difference between the largest and smallest chosen packet.
Constraints
1 <= M <= N <= 100000; 1 <= A[i] <= 1000000000
Example input
7 3 7 3 2 4 9 12 56
Example output
2
Explanation
With M = 3 and packets 7 3 2 4 9 12 56, choosing the packets 2, 3 and 4 gives a difference of 4 - 2 = 2, and no choice of three packets does better.