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:

  1. Each student receives at least one packet of chocolates.
  2. 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

Distribute chocolates among students to minimize the difference between the largest and smallest number of chocolates.

  • Sort the array of chocolates packets in ascending order.

  • Iterate through the array ...read more

Chowdam Mounika
3y

Foe each case print the minimum difference of the chocolates contained in the packets distributed in the student

Anonymous
3y

For each case print the minimum difference of the chocolates contained in the packets distributed to the student.

Select
Add answer anonymously...

Wipro Data Analyst interview questions & answers

A Data Analyst was asked 1mo agoQ. How can artificial intelligence be utilized in data analysis?
A Data Analyst was asked 1mo agoQ. How does artificial intelligence function?
A Data Analyst was asked 2mo agoQ. What is Data Analysis?

Popular interview questions of Data Analyst

A Data Analyst was asked 1mo agoQ1. How can artificial intelligence be utilized in data analysis?
A Data Analyst was asked 2mo agoQ2. What are the three types of golden rules of accounting?
A Data Analyst was asked 2mo agoQ3. What are the different types of golden rules?

Top HR questions asked in Wipro Data Analyst

A Data Analyst was asked 1mo agoQ1. What's your exceptional quality?
A Data Analyst was asked 2mo agoQ2. Why do you need a job?
A Data Analyst was asked 5mo agoQ3. Can you provide an overview of your background, including your past experiences ...read more
Wipro Data Analyst Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits