#K7076. Minimum Wealth Difference Grouping

    ID: 33380 Type: Default 1000ms 256MiB

Minimum Wealth Difference Grouping

Minimum Wealth Difference Grouping

You are given an array of integers representing the wealth of n people and an integer k representing the size of a group. Your task is to form a group of exactly k people such that the difference between the wealthiest and the poorest person in that group is minimized.

If it is not possible to form a group (i.e. when k > n), output -1.

The problem can be formulated as follows: Given a sorted array \(a_0, a_1, \dots, a_{n-1}\), find the value \[ \min_{0\le i \le n-k} \left(a_{i+k-1} - a_i\right) \] which gives the minimum possible difference for any consecutive segment of length k.

inputFormat

The first line of input contains a single integer T — the number of test cases. The description of each test case is as follows:

  1. The first line contains two space-separated integers n and k, where n is the total number of people and k is the size of the group.
  2. The second line contains n space-separated integers representing the wealth values of the people.

outputFormat

For each test case, output a single integer on a new line representing the smallest possible difference between the wealthiest and the poorest person in any group of size k. If forming such a group is impossible, output -1.## sample

1
5 3
1 9 6 4 3
3

</p>