Chocolate Distribution Problem | Practice | GeeksforGeeks

Given an array A[ ] of positive integers of size N, 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 among M students such that :
1. Each student gets exactly one packet.
2. The difference between maximum number of chocolates given to a student and minimum number of chocolates given to a student is minimum.
Example 1:
Input:
N = 8, M = 5
A = {3, 4, 1, 9, 56, 7, 9, 12}
Output: 6
Explanation: The minimum difference between 
maximum chocolates and minimum chocolates 
is 9 - 3 = 6 by choosing following M packets :
{3, 4, 9, 7, 9}.

Example 2:
Input:
N = 7, M = 3
A = {7, 3, 2, 4, 9, 12, 56}
Output: 2
Explanation: The minimum difference between
maximum chocolates and minimum chocolates
is 4 - 2 = 2 by choosing following M packets :
{3, 2, 4}.
Your Task:
You don't need to take any input or print anything. Your task is to complete the function findMinDiff() which takes array A[ ], N and M as input parameters and returns the minimum possible difference between maximum number of chocolates given to a student and minimum number of chocolates given to a student.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 105
1 ≤ Ai ≤ 109
1 ≤ M ≤ N
0
milindprajapatmst194 days ago
# define ll long long
class Solution {
    public:
    ll findMinDiff(vector<ll> arr, int n, int m) {
        sort(arr.begin(), arr.end());
        m--;
        ll result = LLONG_MAX;
        for (ll i = m; i < n; i++)
            result = min(result, arr[i] - arr[i - m]);
        return result;
    }   
};
0
parikshitgaikwad1721 week ago
long long findMinDiff(vector<long long> a, long long n, long long m){
    //code
        sort(a.begin(),a.end());
        long long int diff=INT_MAX;
        for(int i=0;i<n;i++){
            if(i+m-1>=n){
                break;
            }
            long long int sum=(a[i+m-1]-a[i]);
            diff= min(diff,sum);
            
        }
        return diff;
    } 
0
6780apoorvyadav1 week ago
long long findMinDiff(vector<long long> a, long long n, long long m){
    //code
        sort(a.begin(), a.end());
        long long mini = LONG_MAX;
        for(long long i=0;i+m-1<n;i++){
            long long d = a[i+m-1] - a[i];
            mini = min(mini, d);
        }
        return mini;
    }   
0
sourabhkush991 week ago
    long long findMinDiff(vector<long long> a, long long n, long long m){
   sort(a.begin(),a.end());   long long diff = INT_MAX;      for(int i=0; (i+m-1)<=n-1; i++){           if(a[i+m-1]-a[i] < diff){               diff = a[i+m-1]-a[i];           }                 }   return diff;   }   
0
bhavinraichura281 week ago
long long findMinDiff(vector<long long> a, long long n, long long m){   //code       long long min_count=INT_MAX;       sort(a.begin(),a.end());       if (m==1){           return 0;       }       if(n==m){           return a[n-1]-a[0];       }       int j =m-1;       for (int i =0;i<=(n-m);i++){           min_count=min(min_count, a[j]-a[i]);           j++;                  }       return min_count;   }
0
sachinupreti1901 week ago
     vector<int>vec;       unordered_map<int,int>m3;               for(i=0;i<v.size();i++)       {           m3[v[i]]++;       }       for(i=0;i<v.size();i++)       {           if(m3[v[i]]!=-1)           {               vec.push_back(v[i]);               m3[v[i]]=-1;           }       }       return vec;
0
badgujarsachin832 weeks ago
 long long findMinDiff(vector<long long> a, long long n, long long m){
    //code
    sort(a.begin(),a.end());
    int i=0,j=m-1;
    long long di=a[n-1];
    while(j<n){
        di=min(di,a[j]-a[i]);
        i++;j++;
    }
    return di;
    }   
0
nagasivakrishnav2 weeks ago
Python code - Easy short 0.92s:
 
def findMinDiff(self, A,N,M):
       # code here       ans = []       A.sort()       for idx in range(N-M+1):           # 1 3 4 7 9 9 12 56           # 0 1 2 3 4 5 6  7           ans.append(A[idx+M-1]-A[idx])       return min(ans)
0
1712himanshukumar2 weeks ago
class Solution{   public long findMinDiff (ArrayList<Long> a, long n, long m)   {      Collections.sort(a);           long diff=Long.MAX_VALUE;      long i=0;      long j=0;      long ans=Long.MAX_VALUE;      while(j<n && i<n){          if(j-i+1<m){              j++;          }          else if(j-i+1==m){              diff=Math.min(diff,a.get((int)j)-a.get((int)i));  // line a              i++;              j++;                        }          ans=Math.min(diff,ans);      }      return ans;   }}
Tc=o(nlogn)
Sc=o(1)
if we cannot change  long to int , this cause loosy long int error,
so   see in code line a where i convert index long to int.
0
itsmeshahid2 weeks ago
SIMPLE O(nlogn) Solution :
long long findMinDiff(vector<long long> a, long long n, long long m){      sort(a.begin(), a.end());        int i=0;    int j=m-1;        long long diff=a[n-1];//storing maximum value for comparison.        while(j<n){                diff=min(diff, a[j]-a[i]); //compare and store only minimum value.        i++;        j++;            }        return diff;
   }   
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.