#K5346. Minimum Chocolate Difference

    ID: 29536 Type: Default 1000ms 256MiB

Minimum Chocolate Difference

Minimum Chocolate Difference

You are given n houses, each with a certain number of chocolates, and an integer k. Your task is to choose k houses such that the difference between the maximum and minimum number of chocolates among the chosen houses is minimized.

In other words, let \(a_1, a_2, \dots, a_n\) denote the number of chocolates in each house. After sorting these numbers, you need to find a contiguous block of k numbers where the value \[ \min_{i} (a_{i+k-1} - a_i) \] is as small as possible.

Note: The block refers to a group of k consecutive elements in the sorted order of the chocolate counts.

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains two integers, n and k, where n is the number of houses and k is the number of houses to choose.
  • The second line contains n space-separated integers representing the number of chocolates in each house.

outputFormat

Output a single integer on standard output (stdout): the minimum possible difference between the maximum and minimum chocolates among the chosen houses.

## sample
5 2
1 3 2 6 4
1