Problem

Submissions

1

Avg. time to solve

15 min

Success Rate

85%

Problem Statement

```
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.
```

```
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.
```

```
For each test case, print the minimum difference of the chocolates contained in the packets distributed to the students.
```

```
You don't need to print anything, it has already been taken care of. Just implement the given function.
```

```
1 <= T <= 50
2 <= M <= N <= 10^4
1 <= CHOCOLATES[i] <= 10^9
Time Limit : 1 sec
```

```
2
3 2
7 2 4
4 3
3 5 1 6
```

```
2
3
```

```
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β
Hence answer is β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 )
Hence answer is β3β
```

```
2
7 3
7 3 2 4 9 12 56
8 5
3 4 1 9 56 7 9 12
```

```
2
6
```

Console