diff --git a/Array/Chocolate Distribution Problem/Question.txt b/Array/Chocolate Distribution Problem/Question.txt new file mode 100644 index 0000000..7b50276 --- /dev/null +++ b/Array/Chocolate Distribution Problem/Question.txt @@ -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. + diff --git a/Array/Chocolate Distribution Problem/Solution.java b/Array/Chocolate Distribution Problem/Solution.java new file mode 100644 index 0000000..e9983eb --- /dev/null +++ b/Array/Chocolate Distribution Problem/Solution.java @@ -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)); + + } +} +