Chocolate Distribution Problem
Last Updated :
26 Dec, 2023
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 the packet with minimum chocolates given to the students is minimum.
Examples:
Input : arr[] = {7, 3, 2, 4, 9, 12, 56}Â , m = 3Â
Output: Minimum Difference is 2Â
Explanation:
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 = 5Â
Output: Minimum Difference is 6Â
Input : arr[] = {12, 4, 7, 9, 2, 23, 25, 41, 30, 40, 28, 42, 30, 44, 48, 43, 50} , m = 7Â
Output: Minimum Difference is 10Â
Naive Approach for Chocolate Distribution Problem
The idea 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.
Efficient Approach for Chocolate Distribution Problem
The idea 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.
Illustration:
Below is the illustration of example arr[] = [ 7,3,2,4,9,12,56 ] , m = 3
Follow the steps mentioned below to implement the approach:
- Initially sort the given array. And declare a variable to store the minimum difference, and initialize it to INT_MAX. Let the variable be min_diff.
- Find the subarray of size m such that the difference between the last (maximum in case of sorted) and first (minimum in case of sorted) elements of the subarray is minimum.
- We will run a loop from 0 to (n-m), where n is the size of the given array and m is the size of the subarray.
- We will calculate the maximum difference with the subarray and store it in diff = arr[highest index] – arr[lowest index]
- Whenever we get a diff less than the min_diff, we will update the min_diff with diff.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMinDiff( int arr[], int n, int m)
{
if (m == 0 || n == 0)
return 0;
sort(arr, arr + n);
if (n < m)
return -1;
int min_diff = INT_MAX;
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;
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Minimum difference is "
<< findMinDiff(arr, n, m);
return 0;
}
|
C
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
int cmpfunc( const void * a, const void * b)
{
return (*( int *)a - *( int *)b);
}
int findMinDiff( int arr[], int n, int m)
{
if (m == 0 || n == 0)
return 0;
qsort (arr, n, sizeof ( int ), cmpfunc);
if (n < m)
return -1;
int min_diff = INT_MAX;
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;
int n = sizeof (arr) / sizeof (arr[0]);
printf ( "Minimum difference is %d" ,
findMinDiff(arr, n, m));
return 0;
}
|
Java
import java.util.Arrays;
public class ChocolateDistribution {
/**
* Calculates the minimum difference between the maximum and minimum chocolates
* received by any student after distributing chocolates in packets.
*
* @param arr Array representing sizes of chocolate packets.
* @param m Number of students.
* @return Minimum difference between maximum and minimum chocolates.
*/
public static int chocolateDistribution( int arr[], int m) {
if (arr.length == 0 || m == 0 ) {
return 0 ;
}
Arrays.sort(arr);
if (arr.length - 1 < m) {
return - 1 ;
}
int min_diff = Integer.MAX_VALUE;
for ( int i = 0 ; i < arr.length; i++) {
int nextWindow = i + m - 1 ;
if (nextWindow >= arr.length)
break ;
int diff = arr[nextWindow] - arr[i];
min_diff = Math.min(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 ;
int result = chocolateDistribution(arr, m);
if (result != - 1 ) {
System.out.println( "Minimum difference is " + result);
} else {
System.out.println( "Invalid input" );
}
}
}
|
Python3
def findMinDiff(arr, n, m):
if (m = = 0 or n = = 0 ):
return 0
arr.sort()
if (n < m):
return - 1
min_diff = arr[n - 1 ] - arr[ 0 ]
for i in range ( len (arr) - m + 1 ):
min_diff = min (min_diff, arr[i + m - 1 ] - arr[i])
return min_diff
if __name__ = = "__main__" :
arr = [ 12 , 4 , 7 , 9 , 2 , 23 , 25 , 41 ,
30 , 40 , 28 , 42 , 30 , 44 , 48 ,
43 , 50 ]
m = 7
n = len (arr)
print ( "Minimum difference is" , findMinDiff(arr, n, m))
|
C#
using System;
class GFG {
static int findMinDiff( int [] arr, int n, int m)
{
if (m == 0 || n == 0)
return 0;
Array.Sort(arr);
if (n < m)
return -1;
int min_diff = int .MaxValue;
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()
{
int [] arr = { 12, 4, 7, 9, 2, 23, 25, 41, 30,
40, 28, 42, 30, 44, 48, 43, 50 };
int m = 7;
int n = arr.Length;
Console.WriteLine( "Minimum difference is "
+ findMinDiff(arr, n, m));
}
}
|
Javascript
<script>
function findMinDiff(arr, n, m)
{
if (m == 0 || n == 0)
return 0;
arr.sort( function (a, b){ return a - b});
if (n < m)
return -1;
let min_diff = Number.MAX_VALUE;
for (let i = 0; i + m - 1 < n; i++)
{
let diff = arr[i + m - 1] - arr[i];
if (diff < min_diff)
min_diff = diff;
}
return min_diff;
}
let arr = [ 12, 4, 7, 9, 2, 23, 25,
41, 30, 40, 28, 42, 30,
44, 48, 43, 50 ];
let m = 7;
let n = arr.length;
document.write( "Minimum difference is " +
findMinDiff(arr, n, m));
</script>
|
PHP
<?php
function findMinDiff( $arr , $n , $m )
{
if ( $m == 0 || $n == 0)
return 0;
sort( $arr );
if ( $n < $m )
return -1;
$min_diff = PHP_INT_MAX;
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 ;
}
$arr = array (12, 4, 7, 9, 2, 23,
25, 41, 30, 40, 28,
42, 30, 44, 48, 43, 50);
$m = 7;
$n = sizeof( $arr );
echo "Minimum difference is " ,
findMinDiff( $arr , $n , $m );
?>
|
Output
Minimum difference is 10
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...