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
hunterxsameer5 hours ago
class Solution{
    public:
    long long findMinDiff(vector<long long> a, long long n, long long m)
    {
        sort(a.begin(),a.end());
        long long int i=0, j=m-1, ans=INT_MAX;
        while(j<n)
        {
            ans=min(ans,a[j++]-a[i++]);
        }
        return ans;
    }   
};
0
abhishekjune20165 hours ago
long long findMinDiff(vector<long long> a, long long n, long long m){   //code   sort(a.begin(),a.end());   long long mina=INT_MAX;   for(long long i=0;i+m-1<n;i++)   {       if((a[i+m-1]-a[i])<mina)       {           mina=(a[i+m-1]-a[i]);       }   }   return mina;      }   
0
rajkirangupta9916 hours ago
JAVA SOLUTION: -
 
class Solution{   public long findMinDiff (ArrayList<Integer> a, int n, int m)   {       Collections.sort(a);       int min=Integer.MAX_VALUE;       int j=m-1;       int i=0;       while(j<n)       {           min=Math.min(min,a.get(j)-a.get(i));           j++;           i++;       }       return min;   }}
0
apoorvposwal992 days ago
Short C++ solution
long long findMinDiff(vector<long long> a, long long n, long long m){
    sort(a.begin(),a.end());
    long long diff=INT_MAX;
    for(long long i=0,j=m-1;j<n;i++,j++){
        diff=min(diff,a[j]-a[i]);
    }
    return diff;
    } 
0
ritwikshivam642 days ago
In python:def findMinDiff(self, A,N,M):
       # code here       A.sort()       low = 0       high = M-1       diff = max(A)       while(high<N):           diff = min(diff,A[high]-A[low])           low+=1           high+=1       return diff
0
aditichoudharyit192 days ago
long long findMinDiff(vector<long long> a, long long n, long long m){
      sort(a.begin(),a.end());
      long long int ans = INT_MAX;
      int i=0;
      int j=0;
      int p = 0;
      int q = 0;
      long long int x = 0;
      while(j<n)
      {
         if(j-i+1 < m)
            j++;
         else if(j-i+1 == m)
          {
              p =  a[i];
              q = a[j];
              x = q-p;
              ans = min(x,ans);
              i++;j++;
             
          }
      }
      return ans;
      //(o(nlogn)) complexity
   
   }   
+1
vrajeshmodi993 days ago
long long findMinDiff(vector<long long> a, long long n, long long m){
        sort(a.begin(), a.end());
        int i=0,j=m-1;
        long long diff =a[j]-a[i];
        while(j<n){
            diff = min(diff,a[j]-a[i]);
            i++;
            j++;
        }
        return diff;
    }
0
dipanshusharma93133 days ago
// java solution
class Solution{   public long findMinDiff (ArrayList<Integer> a, int n, int m)   {       // your code here       Collections.sort(a);       int p = 0, q = m-1;       long ans = Long.MAX_VALUE;       while(q<=n-1){           int diff = a.get(q) - a.get(p);           ans = Math.min(diff, ans);           q++;           p++;       }       return ans;   }}
0
roboto7o32oo33 days ago
Simple C++ Solution ✨ ✨
 
class Solution {
    public:
    long long findMinDiff(vector<long long> a, long long n, long long m){
        sort(a.begin(), a.end());
        long long result = a[m-1] - a[0];
        
        for(int i=0; i<n-m+1; i++){
            result = min(result, a[m-1+i]-a[i]);
        }
        
        return result;
    }   
};
0
hayatunisha16 days ago
//C++ easy solution
//time taken 0.86/2.73
long long findMinDiff(vector<long long> a, long long n, long long m){   //code       sort(a.begin(),a.end());       int mindiff = INT_MAX;       int tempmin = 0;       for(int i = 0,j = m-1;j<n;i++,j++){           tempmin = a[j]-a[i];           mindiff = min(mindiff,tempmin);       }       return mindiff;   } 
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.