#K82667. Distribute Chocolate Packets
Distribute Chocolate Packets
Distribute Chocolate Packets
Your task is to distribute packets of chocolates among students such that the difference between the packet with the maximum number of chocolates and the packet with the minimum number of chocolates is minimized.
Given an array ( arr ) of ( n ) integers, where each integer represents the number of chocolates in a packet, and a positive integer ( m ) representing the number of students, you need to choose ( m ) packets in such a way that the difference ( (\max - \min) ) among the chosen packets is as small as possible. If there are fewer than ( m ) packets available, output ( -1 ).
The formula to compute the minimum difference after sorting the array is given by:
( \min_{0 \le i \le n-m} { arr[i+m-1] - arr[i] } ).
inputFormat
The first line contains two integers ( n ) and ( m ), where ( n ) is the number of chocolate packets and ( m ) is the number of students. The second line contains ( n ) space-separated integers representing the number of chocolates in each packet.
outputFormat
Print a single integer representing the minimum possible difference between the packet with the maximum chocolates and the packet with the minimum chocolates after distributing to ( m ) students. If ( m ) is greater than ( n ), print ( -1 ).## sample
17 7
12 4 7 9 2 23 25 41 30 40 28 42 30 44 48 43 50
10
</p>