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

Chocolate Distribution Problem By Brajesh #233

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Array/Chocolate Distribution Problem/Question.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Chocolate Distribution Problem
Given an array of n integers 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 such that:

1.)Each student gets one packet.
2.)The difference between the number of chocolates in the packet with maximum chocolates and packet with minimum chocolates given to the students is minimum.

45 changes: 45 additions & 0 deletions Array/Chocolate Distribution Problem/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// JAVA Code For Chocolate Distribution Problem
import java.util.*;

class Chocolate {

static int findMinDiff(int arr[], int n, int m)
{

if (m == 0 || n == 0)
return 0;


Arrays.sort(arr);


if (n < m)
return -1;


int min_diff = Integer.MAX_VALUE;



for (int i = 0; i + m - 1 < n; i++)
{
int diff = arr[i+m-1] - arr[i];
if (diff < min_diff)
min_diff = diff;
}
return min_diff;
}


public static void main(String[] args)
{
int arr[] = {12, 4, 7, 9, 2, 23,25, 41, 30, 40, 28,42, 30, 44, 48, 43,50};

int m = 7; // Number of students

int n = arr.length;
System.out.println("Minimum difference is "+ findMinDiff(arr, n, m));

}
}