1

# Chocolate Problem

Difficulty: MEDIUM
Contributed By
Ayush Thakur
Avg. time to solve
15 min
Success Rate
85%

Problem Statement

#### Example :

``````Given 'N' : 5 (number of packets) and 'M' : 3 (number of students)
``````

``````And chocolates in each packet is : {8, 11, 7, 15, 2}

All possible way to distribute 5 packets of chocolates among 3 students are -

( 8,15, 7 ) difference of maximum-minimum is β15 - 7β = β8β
( 8, 15, 2 ) difference of maximum-minimum is β15 - 2β = β13β
( 8, 15, 11 ) difference of maximum-minimum is β15 - 8β = β7β
( 8, 7, 2 ) difference of maximum-minimum is β8 - 2β = β6β
( 8, 7, 11 ) difference of maximum-minimum is β11 - 7β = β4β
( 8, 2, 11 ) difference of maximum-minimum is β11 - 2β = β9β
( 15, 7, 2 ) difference of maximum-minimum is β15 - 2β = 13β
( 15, 7, 11 ) difference of maximum-minimum is β15 - 7β = β8β
( 15, 2, 11 ) difference of maximum-minimum is β15 - 2β = β13β
( 7, 2, 11 ) difference of maximum-minimum is β11 - 2β = β9β

Hence there are 10 possible ways to distribute β5β packets of chocolate among the β3β students and difference of combination (8, 7, 11) is βmaximum - minimumβ = β11 - 7β = β4β is minimum in all of the above.
``````
##### Input format :
``````The first line of input contains an integer βTβ denoting the number of test cases.
The next β2*Tβ lines represent the βTβ test cases.

The first line of each test case contains two space-separated integers βNβ denoting the number of packets of chocolate and βMβ denotes the number of students.

The second line of each test case contains βNβ space-separated integers denoting the number of chocolate in each of βNβ packets.
``````
##### Output Format :
``````For each test case, print the minimum difference of the chocolates contained in the packets distributed to the students.
``````
##### Note :
``````You don't need to print anything, it has already been taken care of. Just implement the given function.
``````
##### Constraints:
``````1 <= T <= 50
2 <= M <= N <= 10^4
1 <= CHOCOLATES[i] <= 10^9

Time Limit : 1 sec
``````
##### Sample Input 1 :
``````2
3 2
7 2 4
4 3
3 5 1 6
``````
##### Sample Output 1 :
``````2
3
``````
##### Explanation For Sample Input 1 :
``````Test Case 1 :

All possible way to distribute 3 packets of chocolate among 2 students are -

( 7, 2 ) difference is β7 - 2β = β5β
( 7, 4 ) difference is β7 - 4β = β3β
( 2, 4 ) difference is β4 - 2β = β2β

There are three ways to distribute 3 packets of chocolate among 2 students but pair ( 4, 2 ) has minimum difference in βmaximum - minimumβ = β4 - 2β = β2β

Test Case 2 :

All possible way to distribute 4 packets of chocolate among 3 students are -

( 3, 5, 1 )  difference is β5 - 1β = β4β
( 5, 1, 6 )  difference is β6 - 1β = β5β
( 1, 6, 3 )  difference is β6 - 1β = β5β
( 6, 5, 3 )  difference is  β6 - 3β = β3β

There are four options to choose 3 packets of chocolate. Only ( 6, 5, 3 ) pair has the minimum difference β6 - 3β = β3β comparing other pair of difference ( 4, 5, 5 )
``````2
``````2