Given an array arr[] of n integers. The task is to pick exactly m elements such that the maximum difference between any pair of these m elements in minimum.
Examples:
Input: arr[] = {7, 3, 2, 4, 9, 12, 56}, m = 3
Output: 2
Explanation: We pick {3, 2, 4}, we will get the minimum difference, that is 2.Input: arr[] = {7, 3, 2, 4, 9, 12, 56}, m = 5
Output: 7
Explanation: We pick {3, 2, 4, 9, 7}, we will get the minimum difference, that is 9 - 2 = 7.Input : arr[] = {10, 100, 300, 200, 1000, 20, 30}
m = 3
Output : 20
We pick {10, 20, 30}.
max(10, 20, 30) - min(10, 20, 30) = 30 - 10 = 20.
[Naive Approach] By Generating All Subsets
The idea is to generate all subsets of size m of arr[]. For every subset, find the difference between the maximum and minimum elements in it. Finally, return the minimum difference.
[Expected Approach] Using Sliding Window
The idea is to use sliding window technique and choose consecutive elements from a sorted array to minimize the difference. After sorting the array, the difference between the maximum and minimum values in any window of size
mis minimized.First sort the array
arr[]and then use two pointers to maintain a window of sizemto find the subarray with the minimum difference between its last and first elements.
#include <iostream>
#include <vector>
#include <algorithm>
#include <limits.h>
using namespace std;
int findMinDiff(vector<int> &arr, int m) {
int n = arr.size();
sort(arr.begin(), arr.end());
int minDiff = INT_MAX;
for (int i = 0; i + m - 1 < n; i++) {
// calculate difference of current window
int diff = arr[i + m - 1] - arr[i];
// if current difference is smaller
// then update the minimum difference
if (diff < minDiff)
minDiff = diff;
}
return minDiff;
}
int main() {
vector<int> arr = {7, 3, 2, 4, 9, 12, 56};
int m = 3;
cout << findMinDiff(arr, m);
return 0;
}
#include <stdio.h>
#include <limits.h>
// Function to compare two elements for qsort
int compare(const void* a, const void* b) {
return (*(int*)a - *(int*)b);
}
int findMinDiff(int* arr, int n, int m) {
qsort(arr, n, sizeof(int), compare);
int minDiff = INT_MAX;
for (int i = 0; i + m - 1 < n; i++) {
// calculate difference of current window
int diff = arr[i + m - 1] - arr[i];
// if current difference is smaller
// then update the minimum difference
if (diff < minDiff)
minDiff = diff;
}
return minDiff;
}
int main() {
int arr[] = {7, 3, 2, 4, 9, 12, 56};
int n = sizeof(arr) / sizeof(arr[0]);
int m = 3;
printf("%d\n", findMinDiff(arr, n, m));
return 0;
}
import java.util.Arrays;
class GfG {
static int findMinDiff(int[] arr, int m) {
int n = arr.length;
Arrays.sort(arr);
int minDiff = Integer.MAX_VALUE;
for (int i = 0; i + m - 1 < n; i++) {
// calculate difference of current window
int diff = arr[i + m - 1] - arr[i];
// if current difference is smaller
// then update the minimum difference
if (diff < minDiff)
minDiff = diff;
}
return minDiff;
}
public static void main(String[] args) {
int[] arr = {7, 3, 2, 4, 9, 12, 56};
int m = 3;
System.out.println(findMinDiff(arr, m));
}
}
def findMinDiff(arr, m):
n = len(arr)
arr.sort()
minDiff = float('inf')
for i in range(n - m + 1):
# calculate difference of current window
diff = arr[i + m - 1] - arr[i]
# if current difference is smaller
# then update the minimum difference
if diff < minDiff:
minDiff = diff
return minDiff
if __name__ == "__main__":
arr = [7, 3, 2, 4, 9, 12, 56]
m = 3
print(findMinDiff(arr, m))
using System;
class GfG {
static int findMinDiff(int[] arr, int m) {
int n = arr.Length;
Array.Sort(arr);
int minDiff = int.MaxValue;
for (int i = 0; i + m - 1 < n; i++) {
// calculate difference of current window
int diff = arr[i + m - 1] - arr[i];
// if current difference is smaller
// then update the minimum difference
if (diff < minDiff)
minDiff = diff;
}
return minDiff;
}
static void Main() {
int[] arr = {7, 3, 2, 4, 9, 12, 56};
int m = 3;
Console.WriteLine(findMinDiff(arr, m));
}
}
function findMinDiff(arr, m) {
let n = arr.length;
arr.sort((a, b) => a - b);
let minDiff = Number.MAX_SAFE_INTEGER;
for (let i = 0; i + m - 1 < n; i++) {
// calculate difference of current window
let diff = arr[i + m - 1] - arr[i];
// if current difference is smaller
// then update the minimum difference
if (diff < minDiff)
minDiff = diff;
}
return minDiff;
}
let arr = [7, 3, 2, 4, 9, 12, 56];
let m = 3;
console.log(findMinDiff(arr, m));
Output
2
Time Complexity: n*log(n), where n is the size of arr[].
Auxiliary Space: O(1)