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
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.
Starting 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 subsetint 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 codeint 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 sizeimport 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. |
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 subsetdef 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 Codearr = [10, 20, 30, 100, 101, 102]N = len(arr)K = 3print(minDifferenceAmongMaxMin(arr, N, K))# This code is contributed by Anant Agarwal. |
C#
// C# program to find minimum difference// between max and min of all subset of// K sizeusing 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 |
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 subsetfunction 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.?> |
Javascript
<script>// JavaScript 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) { // sort the array so that close // elements come together. arr.sort((a, b) => a - b); // initialize result by // a big integer number let res = 2147483647; // loop over first (N - K) elements // of the array only for (let i = 0; i <= (N - K); i++) { // get difference between max and // min of current K-sized segment let curSeqDiff = arr[i + K - 1] - arr[i]; res = Math.min(res, curSeqDiff); } return res; }// Driver Code let arr = [10, 20, 30, 100, 101, 102]; let N = arr.length; let K = 3; document.write( minDifferenceAmongMaxMin(arr, N, K));</script> |
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 write.geeksforgeeks.org or mail your article to review-team@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.



