Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Sweet Success: Revolutionizing Chocolate Distribution"🍫 #32

Open
MaRia19280 opened this issue Jan 4, 2025 · 4 comments
Open

"Sweet Success: Revolutionizing Chocolate Distribution"🍫 #32

MaRia19280 opened this issue Jan 4, 2025 · 4 comments
Labels
challenge a new challenge

Comments

@MaRia19280
Copy link

Hello Team!!!

Here is the Problem,

**_*Problem Description:

You have a list of packets, each containing a certain number of chocolates. Your task is to distribute these packets to k students in such a way that the difference between the largest and smallest number of chocolates received is as small as possible.

For example, given the packets [12, 4, 7, 9] and k = 2, you can distribute [7, 9] to minimize the difference, which is 9 - 7 = 2.

Write a Python function to solve this problem efficiently._**

Problem⬇️

File: solutions/chocolate_distribution.py

def minimize_chocolate_difference(chocolates, k):
"""
Function to minimize the difference between the maximum and minimum chocolates
distributed among students.

Parameters:
chocolates (list): List of integers representing chocolates in packets.
k (int): Number of students.

Returns:
int: The minimized difference.
"""
if k == 0 or len(chocolates) == 0:
    return 0

if len(chocolates) < k:
    return -1  # Not enough packets for the students

# Sort the chocolate packets
chocolates.sort()

# Find the minimum difference
min_diff = float('inf')
for i in range(len(chocolates) - k + 1):
    diff = chocolates[i + k - 1] - chocolates[i]
    min_diff = min(min_diff, diff)

return min_diff

Example usage

if name == "main":
chocolates = [12, 4, 7, 9, 2, 23, 25, 41, 30, 40, 28, 42, 30, 44, 48, 43, 50]
k = 7
result = minimize_chocolate_difference(chocolates, k)
print(f"Minimum difference is {result}")

@MaRia19280 MaRia19280 changed the title "Sweet Success: Revolutionizing Chocolate Distribution" "Sweet Success: Revolutionizing Chocolate Distribution"🍫 Jan 4, 2025
@abdoalsir
Copy link

Hi Maria,
can you please create a pull request with these files so we can review them with the code review checklist

@MaRia19280
Copy link
Author

Yeah of course, very soon I'll do it, thank for reminding

@MaherAssaf19
Copy link

Hi Maria,

Nice job. It would be a good idea if you edited by mentioning the distribution around the countries-continents or even the consumption of it. What do you think?

@MuhannadGTR
Copy link

Hi Maria,

Are you focusing on general chocolate distribution or on a certain type? for ex. milk, dark, etc.? Your explanation is more generalized, but I'm not sure if thats what you meant as well.

@malakbattat malakbattat added the challenge a new challenge label Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
challenge a new challenge
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

5 participants