Chocolate Distribution Problem
You are given an array/list CHOCOLATES
of size 'N', where each element represents the number of chocolates in a packet. Your task is to distribute these chocolates among 'M' students such that:
- Each student receives at least one packet of chocolates.
- The difference between the largest and smallest number of chocolates in the distributed packets is minimized.
Input:
T
: number of test cases
For each test case:
N
M
: two space-separated integers denoting the number of packets and number of students
CHOCOLATES
: 'N' space-separated integers representing chocolates in each packet
Output:
For each test case, print the minimum possible difference between the maximum and minimum number of chocolates in packets, distributed to students.
Example:
Input:
T = 1
N = 5, M = 3
CHOCOLATES = [8, 11, 7, 15, 2]
Output:
4
Constraints:
1 <= T <= 50
2 <= M <= N <= 10^4
1 <= CHOCOLATES[i] <= 10^9
Note:
You are not required to print anything. Implement the given function to return the required result.

AnswerBot
4mo
The task is to distribute chocolates among students such that the difference between the largest and smallest number of chocolates is minimized.
Sort the array of chocolates packets
Use sliding window t...read more
Help your peers!
Add answer anonymously...
Tech Vedika Software Engineer interview questions & answers
A Software Engineer was asked Q. What are some basic questions you were asked about JavaScript?
A Software Engineer was asked Q. Left Rotations of an Array You are given an array consisting of N elements and n...read more
A Software Engineer was asked Q. Ways To Make Coin Change Given an infinite supply of coins of varying denominati...read more
Popular interview questions of Software Engineer
A Software Engineer was asked Q1. What are some basic questions you were asked about JavaScript?
A Software Engineer was asked Q2. Left Rotations of an Array You are given an array consisting of N elements and n...read more
A Software Engineer was asked Q3. Ways To Make Coin Change Given an infinite supply of coins of varying denominati...read more
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

