-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chocolate distribution.py
48 lines (40 loc) · 1.43 KB
/
Chocolate distribution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
''' Given an array A of positive integers of size N, where each value represents number of chocolates in a packet. Each packet can have variable number of chocolates. There are M students, the task is to distribute chocolate packets such that :
1. Each student gets one packet.
2. The difference between the number of chocolates given to the students having packet with maximum chocolates and student having packet with minimum chocolates is minimum.
Input:
The first line of input contains an integer T, denoting the number of test cases. Then T test cases follow. Each test case consists of three lines. The first line of each test case contains an integer N denoting the number of packets. Then next line contains N space separated values of the array A denoting the values of each packet. The third line of each test case contains an integer m denoting the no of students.
Output:
For each test case in a new line print the minimum difference.
Constraints:
1 <= T <= 100
1 <=N<= 107
1 <= Ai <= 1018
1 <= M <= N
Example:
Input:
2
8
3 4 1 9 56 7 9 12
5
7
7 3 2 4 9 12 56
3
Output:
6
2 '''
for _ in range(int(input())):
n = int(input())
l = list(map(int,input().split()))
m = int(input())
min_d = 100000
l.sort()
i = 0
j = m-1
if(m!=0 and n!=0 and n>=m):
while(j<n):
d = l[j]-l[i]
if d<min_d:
min_d = d
j += 1
i += 1
print(min_d)