Skip to content

Commit 77d8539

Browse files
authored
Merge pull request #98 from dsrao711/issue-91
Chocolate Distribution Problem-Python
2 parents c95f9ce + 431010a commit 77d8539

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

DSA 450 GFG/chocolate_distribution.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Problem : https://practice.geeksforgeeks.org/problems/chocolate-distribution-problem3825/1#
2+
3+
#Given an array A[ ] of positive integers of size N, where each value represents the number of chocolates in a packet. Each packet can have a variable number of chocolates. There are M students, the task is to distribute chocolate packets among M students such that :
4+
#1. Each student gets exactly one packet.
5+
#2. The difference between maximum number of chocolates given to a student and minimum number of chocolates given to a student is minimum.
6+
7+
8+
class Solution:
9+
10+
def findMinDiff(self, A,N,M):
11+
12+
# code here
13+
if(N == 0 or M == 0):
14+
return 0
15+
elif (N < M):
16+
return -1
17+
else:
18+
A.sort()
19+
min_diff = A[N-1] - A[0]
20+
for i in range(0 , N - M + 1):
21+
min_diff = min(min_diff , A[i+M-1] - A[i])
22+
return min_diff
23+
24+
25+
#{
26+
# Driver Code Starts
27+
#Initial Template for Python 3
28+
29+
if __name__ == '__main__':
30+
31+
t = int(input())
32+
33+
for _ in range(t):
34+
N = int(input())
35+
A = [int(x) for x in input().split()]
36+
M = int(input())
37+
38+
39+
solObj = Solution()
40+
41+
print(solObj.findMinDiff(A,N,M))
42+
# } Driver Code Ends

0 commit comments

Comments
 (0)