#K63567. Minimum Brightness Difference
Minimum Brightness Difference
Minimum Brightness Difference
Sarah has a collection of n colors, each with a given brightness level. She wants to choose k colors such that the difference between the brightest and the dimmest among these selected colors is minimized. To achieve this, one can sort the brightness levels and then consider every contiguous segment of k values in the sorted order.
For instance, if the brightness levels are \( [10, 20, 30, 40, 50] \) and \( k = 2 \), the best choice is to pick [10, 20] or any adjacent pair, resulting in a minimum difference of 10.
Your task is to implement an efficient algorithm to compute this minimum difference.
inputFormat
The input is read from standard input (stdin) and consists of two lines. The first line contains two integers, n and k, where n is the number of colors and k is the number of colors to select. The second line contains n space-separated integers representing the brightness levels of the colors.
outputFormat
Output a single integer to standard output (stdout) representing the minimum difference between the brightest and the dimmest colors among any valid selection of k colors.## sample
5 2
10 20 30 40 50
10