# Minimum difference between max and min of all K-size subsets

Given an array of integer values, we need to find the minimum difference between maximum and minimum of all possible K-length subsets.
Examples:

```Input : arr[] = [3, 5, 100, 101, 102]
K = 3
Output : 2
Possible subsets of K-length with
their differences are,
[3 5 100]  max min diff is (100 - 3) = 97
[3 5 101]  max min diff is (101 - 3) = 98
[3 5 102]  max min diff is (102 - 3) = 99
[3 100 101]  max min diff is (101 - 3) = 98
[3 100 102]  max min diff is (102 - 3) = 99
[3 101 102]  max min diff is (102 - 3) = 98
[5 100 101]  max min diff is (101 - 5) = 96
[5 100 102]  max min diff is (102 - 5) = 97
[5 101 102]  max min diff is (102 - 5) = 97
[100 101 102] max min diff is (102 - 100) = 2
As the minimum difference is 2, it should
be the answer for given array.

Input : arr[] = {5, 1, 10, 6}
k = 2
Output : 1
We get the above result considering subset
{5, 6}
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

We can solve this problem without iterating over all possible subsets by observing the fact that our result subset will always be consecutive, once we sort the given array. The reason is sorting brings value-wise close elements together.
We can prove above fact as follows – Suppose we chose number a1, a2, a3 … aK which are in increasing order but not continuous, then our difference will be (aK – a1) but if we include the number which was not taken earlier (let aR) then our K length subset will be a2, a3, … aR, …. aK. In this case, our difference will (aK – a2) which must be smaller than (aK – a1) because a2 > a1. So we can say that the subset which will contain our answer will always be consecutive in sorted array.
Stating above fact, for solving the problem first we sort the array then we will iterate over first (N – K) elements and each time we will take the difference between elements which are K distant apart and our final answer will be minimum of them.

```// C++ program to find minimum difference
// between max and min of all subset of K size
#include <bits/stdc++.h>
using namespace std;

// returns min difference between max
// and min of any K-size subset
int minDifferenceAmongMaxMin(int arr[], int N, int K)
{
// sort the array so that close
// elements come together.
sort(arr, arr + N);

//  initialize result by a big integer number
int res = INT_MAX;

// loop over first (N - K) elements
// of the array only
for (int i = 0; i <= (N - K); i++)
{
//  get difference between max and min of
// current K-sized segment
int curSeqDiff = arr[i + K - 1] - arr[i];
res = min(res, curSeqDiff);
}

return res;
}

//  Driver code to test above methods
int main()
{
int arr[] = {10, 20, 30, 100, 101, 102};
int N = sizeof(arr)/sizeof(arr[0]);

int K = 3;
cout << minDifferenceAmongMaxMin(arr, N, K);
return 0;
}
```

Output:

```2
```

Time Complexity : O(n Log n)

This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
1.4 Average Difficulty : 1.4/5.0
Based on 9 vote(s)