You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
longlongfindMinDiff(vector<longlong> a, longlong n, longlong m){
//codeif (m == 0 || n == 0) {
return0;
}
// Sort the array to arrange packets in increasing order of chocolatessort(a.begin(), a.end());
// If there are fewer packets than students, return -1 as we cannot distributeif (n < m) {
return -1;
}
// Initialize the minimum difference as a large numberlonglong minDiff = LLONG_MAX;
// Iterate over all possible windows of size Mfor (longlong i = 0; i + m - 1 < n; i++) {
// Calculate the difference between the maximum and minimum chocolates in the current windowlonglong diff = a[i + m - 1] - a[i];
// Update the minimum difference if the current difference is smallerif (diff < minDiff) {
minDiff = diff;
}
}
return minDiff;
}