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:

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

**Examples:**

Input :arr[] = {7, 3, 2, 4, 9, 12, 56} , m = 3Output:Minimum Difference is 2Explanation:

We have seven packets of chocolates and

we need to pick three packets for 3 students

If we pick 2, 3 and 4, we get the minimum

difference between maximum and minimum packet

sizes.

Input :arr[] = {3, 4, 1, 9, 56, 7, 9, 12} , m = 5Output:Minimum Difference is 6Explanation:

The set goes like 3,4,7,9,9 and the output

is 9-3 = 6

Input :arr[] = {12, 4, 7, 9, 2, 23, 25, 41,

30, 40, 28, 42, 30, 44, 48,

43, 50} , m = 7Output:Minimum Difference is 10Explanation:

We need to pick 7 packets. We pick 40, 41,

42, 44, 48, 43 and 50 to minimize difference

between maximum and minimum.

Source: Flipkart Interview Experience

A **simple solution** is to generate all subsets of size m of arr[0..n-1]. For every subset, find the difference between the maximum and minimum elements in it. Finally, return the minimum difference.

An **efficient solution** is based on the observation that to minimize the difference, we must choose consecutive elements from a sorted packet. We first sort the array arr[0..n-1], then find the subarray of size m with the minimum difference between the last and first elements.

Below image is a dry run of the above approach:

Below is the implementation of the above approach:

## C++

`// C++ program to solve chocolate distribution` `// problem` `#include<bits/stdc++.h>` `using` `namespace` `std;` `// arr[0..n-1] represents sizes of packets` `// m is number of students.` `// Returns minimum difference between maximum` `// and minimum values of distribution.` `int` `findMinDiff(` `int` `arr[], ` `int` `n, ` `int` `m)` `{` ` ` `// if there are no chocolates or number` ` ` `// of students is 0` ` ` `if` `(m==0 || n==0)` ` ` `return` `0;` ` ` `// Sort the given packets` ` ` `sort(arr, arr+n);` ` ` `// Number of students cannot be more than` ` ` `// number of packets` ` ` `if` `(n < m)` ` ` `return` `-1;` ` ` `// Largest number of chocolates` ` ` `int` `min_diff = INT_MAX;` ` ` `// Find the subarray of size m such that` ` ` `// difference between last (maximum in case` ` ` `// of sorted) and first (minimum in case of` ` ` `// sorted) elements of subarray is minimum.` ` ` ` ` `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;` `}` `int` `main()` `{` ` ` `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 = ` `sizeof` `(arr)/` `sizeof` `(arr[0]);` ` ` `cout << ` `"Minimum difference is "` ` ` `<< findMinDiff(arr, n, m);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// JAVA Code For Chocolate Distribution ` `// Problem` `import` `java.util.*;` `class` `GFG {` ` ` ` ` `// arr[0..n-1] represents sizes of` ` ` `// packets. m is number of students.` ` ` `// Returns minimum difference between` ` ` `// maximum and minimum values of ` ` ` `// distribution.` ` ` `static` `int` `findMinDiff(` `int` `arr[], ` `int` `n, ` ` ` `int` `m)` ` ` `{` ` ` `// if there are no chocolates or ` ` ` `// number of students is 0` ` ` `if` `(m == ` `0` `|| n == ` `0` `)` ` ` `return` `0` `;` ` ` ` ` `// Sort the given packets` ` ` `Arrays.sort(arr);` ` ` ` ` `// Number of students cannot be` ` ` `// more than number of packets` ` ` `if` `(n < m)` ` ` `return` `-` `1` `;` ` ` ` ` `// Largest number of chocolates` ` ` `int` `min_diff = Integer.MAX_VALUE;` ` ` ` ` `// Find the subarray of size m ` ` ` `// such that difference between ` ` ` `// last (maximum in case of ` ` ` `// sorted) and first (minimum in` ` ` `// case of sorted) elements of ` ` ` `// subarray is minimum.` ` ` ` ` `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;` ` ` `}` ` ` ` ` `/* Driver program to test above function */` ` ` `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));` ` ` ` ` `}` `}` `// This code is contributed by Arnav Kr. Mandal.` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to solve ` `# chocolate distribution` `# problem` `import` `sys;` `# arr[0..n-1] represents sizes of packets` `# m is number of students.` `# Returns minimum difference between maximum` `# and minimum values of distribution.` `def` `findMinDiff(arr, n, m):` ` ` `# if there are no chocolates or number` ` ` `# of students is 0` ` ` `if` `(m` `=` `=` `0` `or` `n` `=` `=` `0` `):` ` ` `return` `0` ` ` `# Sort the given packets` ` ` `arr.sort()` ` ` `# Number of students cannot be more than` ` ` `# number of packets` ` ` `if` `(n < m):` ` ` `return` `-` `1` ` ` `# Largest number of chocolates` ` ` `min_diff ` `=` `sys.maxsize` ` ` `# Find the subarray of size m such that` ` ` `# difference between last (maximum in case` ` ` `# of sorted) and first (minimum in case of` ` ` `# sorted) elements of subarray is minimum.` ` ` `i` `=` `0` ` ` `while` `(i` `+` `m` `-` `1` `<n ):` ` ` ` ` `diff ` `=` `arr[i` `+` `m` `-` `1` `] ` `-` `arr[i]` ` ` `if` `(diff < min_diff):` ` ` `min_diff ` `=` `diff` ` ` ` ` `i` `+` `=` `1` ` ` ` ` `return` `min_diff` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` `arr ` `=` `[` `12` `, ` `4` `, ` `7` `, ` `9` `, ` `2` `, ` `23` `, ` `25` `, ` `41` `,` ` ` `30` `, ` `40` `, ` `28` `, ` `42` `, ` `30` `, ` `44` `, ` `48` `, ` ` ` `43` `, ` `50` `]` ` ` `m ` `=` `7` `# Number of students` ` ` `n ` `=` `len` `(arr)` ` ` `print` `(` `"Minimum difference is"` `, findMinDiff(arr, n, m))` ` ` `#This code is contributed by Smitha` |

*chevron_right*

*filter_none*

## C#

`// C# Code For Chocolate Distribution ` `// Problem` `using` `System;` `class` `GFG {` ` ` ` ` `// arr[0..n-1] represents sizes of` ` ` `// packets. m is number of students.` ` ` `// Returns minimum difference between` ` ` `// maximum and minimum values of ` ` ` `// distribution.` ` ` `static` `int` `findMinDiff(` `int` `[]arr, ` `int` `n, ` ` ` `int` `m)` ` ` `{` ` ` ` ` `// if there are no chocolates or ` ` ` `// number of students is 0` ` ` `if` `(m == 0 || n == 0)` ` ` `return` `0;` ` ` ` ` `// Sort the given packets` ` ` `Array.Sort(arr);` ` ` ` ` `// Number of students cannot be` ` ` `// more than number of packets` ` ` `if` `(n < m)` ` ` `return` `-1;` ` ` ` ` `// Largest number of chocolates` ` ` `int` `min_diff = ` `int` `.MaxValue;` ` ` ` ` `// Find the subarray of size m ` ` ` `// such that difference between ` ` ` `// last (maximum in case of ` ` ` `// sorted) and first (minimum in` ` ` `// case of sorted) elements of ` ` ` `// subarray is minimum.` ` ` ` ` `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;` ` ` `}` ` ` ` ` `/* Driver program to test above function */` ` ` `public` `static` `void` `Main() ` ` ` `{` ` ` `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;` ` ` ` ` `Console.WriteLine(` `"Minimum difference is "` ` ` `+ findMinDiff(arr, n, m));` ` ` ` ` `}` `}` `// This code is contributed by vt_m.` |

*chevron_right*

*filter_none*

## PHP

`<?php` `// PHP program to solve ` `// chocolate distribution` `// problem` `// arr[0..n-1] represents ` `// sizes of packets m is` `// number of students.` `// Returns minimum difference ` `// between maximum and minimum ` `// values of distribution.` `function` `findMinDiff(` `$arr` `, ` `$n` `, ` `$m` `)` `{` ` ` `// if there are no ` ` ` `// chocolates or number` ` ` `// of students is 0` ` ` `if` `(` `$m` `== 0 || ` `$n` `== 0)` ` ` `return` `0;` ` ` `// Sort the given packets` ` ` `sort(` `$arr` `);` ` ` `// Number of students` ` ` `// cannot be more than` ` ` `// number of packets` ` ` `if` `(` `$n` `< ` `$m` `)` ` ` `return` `-1;` ` ` `// Largest number ` ` ` `// of chocolates` ` ` `$min_diff` `= PHP_INT_MAX;` ` ` `// Find the subarray of size` ` ` `// m such that difference` ` ` `// between last (maximum in ` ` ` `// case of sorted) and first ` ` ` `// (minimum in case of sorted) ` ` ` `// elements of subarray is minimum.` ` ` ` ` `for` `(` `$i` `= 0; ` ` ` `$i` `+ ` `$m` `- 1 < ` `$n` `; ` `$i` `++)` ` ` `{` ` ` `$diff` `= ` `$arr` `[` `$i` `+ ` `$m` `- 1] -` ` ` `$arr` `[` `$i` `];` ` ` `if` `(` `$diff` `< ` `$min_diff` `)` ` ` `$min_diff` `= ` `$diff` `;` ` ` `}` ` ` `return` `$min_diff` `;` `}` `// Driver Code` `$arr` `= ` `array` `(12, 4, 7, 9, 2, 23, ` ` ` `25, 41, 30, 40, 28, ` ` ` `42, 30, 44, 48, 43, 50);` ` ` `$m` `= 7; ` `// Number of students` `$n` `= sizeof(` `$arr` `);` `echo` `"Minimum difference is "` `,` ` ` `findMinDiff(` `$arr` `, ` `$n` `, ` `$m` `);` `// This code is contributed by ajit` `?>` |

*chevron_right*

*filter_none*

**Output**

Minimum difference is 10

**Time Complexity:** O(n Log n) as we apply sorting before subarray search.

This article is contributed by **Roshni Agarwal**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Nuts & Bolts Problem (Lock & Key problem) | Set 1
- Nuts & Bolts Problem (Lock & Key problem) | Set 2 (Hashmap)
- Distribution of a Number in Array within a Range
- Activity Selection Problem | Greedy Algo-1
- Subset Sum Problem | DP-25
- A Pancake Sorting Problem
- Snake and Ladder Problem
- Gold Mine Problem
- Perfect Sum Problem (Print all subsets with given sum)
- Subset Sum Problem in O(sum) space
- Job Sequencing Problem - Loss Minimization
- Program for Bridge and Torch problem
- Job Selection Problem - Loss Minimization Strategy | Set 2
- Print equal sum sets of array (Partition problem) | Set 1
- Print equal sum sets of array (Partition Problem) | Set 2
- Multiset Equivalence Problem
- Water drop problem
- Flipping Sign Problem | Lazy Propagation Segment Tree
- 21 Matchsticks Problem
- Prime Subset Product Problem