Given an array of integer values, we need to find the minimum difference between the 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 choose 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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int minDifferenceAmongMaxMin( int arr[], int N, int K)
{
sort(arr, arr + N);
int res = INT_MAX;
for ( int i = 0; i <= (N - K); i++)
{
int curSeqDiff = arr[i + K - 1] - arr[i];
res = min(res, curSeqDiff);
}
return res;
}
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
import java.util.Arrays;
class GFG
{
static int minDifferenceAmongMaxMin( int arr[],
int N, int K)
{
Arrays.sort(arr);
int res = 2147483647 ;
for ( int i = 0 ; i <= (N - K); i++)
{
int curSeqDiff = arr[i + K - 1 ] - arr[i];
res = Math.min(res, curSeqDiff);
}
return res;
}
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));
}
}
|
Python3
def minDifferenceAmongMaxMin(arr, N, K):
arr.sort()
res = 2147483647
for i in range ((N - K) + 1 ):
curSeqDiff = arr[i + K - 1 ] - arr[i]
res = min (res, curSeqDiff)
return res
arr = [ 10 , 20 , 30 , 100 , 101 , 102 ]
N = len (arr)
K = 3
print (minDifferenceAmongMaxMin(arr, N, K))
|
C#
using System;
class GFG
{
static int minDifferenceAmongMaxMin( int []arr,
int N, int K)
{
Array.Sort(arr);
int res = 2147483647;
for ( int i = 0; i <= (N - K); i++)
{
int curSeqDiff = arr[i + K - 1] - arr[i];
res = Math.Min(res, curSeqDiff);
}
return res;
}
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));
}
}
|
PHP
<?php
function minDifferenceAmongMaxMin( $arr , $N ,
$K )
{
$INT_MAX = 2;
sort( $arr ); sort( $arr , $N );
$res = $INT_MAX ;
for ( $i = 0; $i <= ( $N - $K ); $i ++)
{
$curSeqDiff = $arr [ $i + $K - 1] -
$arr [ $i ];
$res = min( $res , $curSeqDiff );
}
return $res ;
}
$arr = array (10, 20, 30, 100, 101, 102);
$N = sizeof( $arr );
$K = 3;
echo minDifferenceAmongMaxMin( $arr , $N , $K );
?>
|
Javascript
<script>
function minDifferenceAmongMaxMin(arr, N, K)
{
arr.sort((a, b) => a - b);
let res = 2147483647;
for (let i = 0; i <= (N - K); i++)
{
let curSeqDiff = arr[i + K - 1] - arr[i];
res = Math.min(res, curSeqDiff);
}
return res;
}
let arr = [10, 20, 30, 100, 101, 102];
let N = arr.length;
let K = 3;
document.write(
minDifferenceAmongMaxMin(arr, N, K));
</script>
|
Time Complexity: O(n Log n)
Auxiliary Space: O(1)
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.