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 Explanation : 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 ` `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; ` ` ` `} ` |

*chevron_right*

*filter_none*

## 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 code ` ` ` `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. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find minimum ` `# difference between max and min ` `# of all subset of K size ` ` ` `# Returns min difference between max ` `# and min of any K-size subset ` `def` `minDifferenceAmongMaxMin(arr, N, K): ` ` ` ` ` `# sort the array so that close ` ` ` `# elements come together. ` ` ` `arr.sort() ` ` ` ` ` `# initialize result by a ` ` ` `# big integer number ` ` ` `res ` `=` `2147483647` ` ` ` ` `# loop over first (N - K) elements ` ` ` `# of the array only ` ` ` `for` `i ` `in` `range` `((N ` `-` `K) ` `+` `1` `): ` ` ` ` ` `# get difference between max and min ` ` ` `# of current K-sized segment ` ` ` `curSeqDiff ` `=` `arr[i ` `+` `K ` `-` `1` `] ` `-` `arr[i] ` ` ` `res ` `=` `min` `(res, curSeqDiff) ` ` ` ` ` `return` `res ` ` ` `# Driver Code ` `arr ` `=` `[` `10` `, ` `20` `, ` `30` `, ` `100` `, ` `101` `, ` `102` `] ` `N ` `=` `len` `(arr) ` `K ` `=` `3` `print` `(minDifferenceAmongMaxMin(arr, N, K)) ` ` ` `# This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find minimum difference ` `// between max and min of all subset of ` `// K size ` `using` `System; ` ` ` `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. ` ` ` `Array.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 code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[]arr= {10, 20, 30, 100, 101, 102}; ` ` ` `int` `N = arr.Length; ` ` ` ` ` `int` `K = 3; ` ` ` `Console.Write( ` ` ` `minDifferenceAmongMaxMin(arr, N, K)); ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find minimum difference ` `// between max and min of all subset ` `// of K size ` ` ` `// returns min difference between max ` `// and min of any K-size subset ` `function` `minDifferenceAmongMaxMin(` `$arr` `, ` `$N` `, ` ` ` `$K` `) ` `{ ` ` ` `$INT_MAX` `= 2; ` ` ` ` ` `// sort the array so that close ` ` ` `// elements come together. ` ` ` `sort(` `$arr` `); sort(` `$arr` `, ` `$N` `); ` ` ` ` ` `// initialize result by a ` ` ` `// big integer number ` ` ` `$res` `= ` `$INT_MAX` `; ` ` ` ` ` `// loop over first (N - K) elements ` ` ` `// of the array only ` ` ` `for` `(` `$i` `= 0; ` `$i` `<= (` `$N` `- ` `$K` `); ` `$i` `++) ` ` ` `{ ` ` ` ` ` `// get difference between max and ` ` ` `// min of current K-sized segment ` ` ` `$curSeqDiff` `= ` `$arr` `[` `$i` `+ ` `$K` `- 1] - ` ` ` `$arr` `[` `$i` `]; ` ` ` `$res` `= min(` `$res` `, ` `$curSeqDiff` `); ` ` ` `} ` ` ` ` ` `return` `$res` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$arr` `= ` `array` `(10, 20, 30, 100, 101, 102); ` ` ` `$N` `= sizeof(` `$arr` `); ` ` ` ` ` `$K` `= 3; ` ` ` `echo` `minDifferenceAmongMaxMin(` `$arr` `, ` `$N` `, ` `$K` `); ` ` ` `// This code is contributed by Nitin Mittal. ` `?> ` |

*chevron_right*

*filter_none*

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- k size subsets with maximum difference d between max and min
- Partition a set into two subsets such that difference between max of one and min of other is minimized
- Count of subsets having sum of min and max element less than K
- Split Array into min number of subsets with difference between each pair greater than 1
- Sum of width (max and min diff) of all Subsequences
- Divide a sorted array in K parts with sum of difference of max and min minimized in each part
- Longest subarray such that difference of max and min is at-most K
- Sum of subsets of all the subsets of an array | O(3^N)
- Sum of subsets of all the subsets of an array | O(2^N)
- Sum of subsets of all the subsets of an array | O(N)
- Remove minimum elements from the array such that 2*min becomes more than max
- Minimize count of Subsets with difference between maximum and minimum element not exceeding K
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Sum of (maximum element - minimum element) for all the subsets of an array.
- Split N powers of 2 into two subsets such that their difference of sum is minimum
- Maximize the difference between two subsets of a set with negatives
- Minimum difference between maximum and minimum value of Array with given Operations
- Count minimum number of subsets (or subsequences) with consecutive numbers
- Minimum number of subsets with distinct elements
- Sum of average of all subsets