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}

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++

// 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; }

## Java

// Java program to find minimum difference // between max and min of all subset of // K size import java.util.Arrays; class GFG { // returns min difference between max // and min of any K-size subset static int minDifferenceAmongMaxMin(int arr[], int N, int K) { // sort the array so that close // elements come together. Arrays.sort(arr); // initialize result by a big integer // number int res = 2147483647; // 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 = Math.min(res, curSeqDiff); } return res; } // Driver method public static void main(String[] args) { int arr[] = {10, 20, 30, 100, 101, 102}; int N = arr.length; int K = 3; System.out.print( minDifferenceAmongMaxMin(arr, N, K)); } } // This code is contributed by Anant Agarwal.

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.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.