给定一个整数值数组,我们需要找到所有可能的K长度子集的最大值和最小值之间的最小差。
例子 :
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}
我们可以解决这个问题,而无需遍历所有可能的子集,只要对给定数组进行排序,我们的结果子集将始终是连续的这一事实。原因是排序将按价值计算的紧密元素组合在一起。
我们可以证明上述事实如下-假设我们选择的数字a1,a2,a3…aK顺序递增但不连续,那么我们的差将是(aK – a1),但是如果我们包括未早先采用的数字(令aR),那么我们的K长度子集将为a2,a3,… aR,…。 K在这种情况下,我们的差(aK – a2)必须小于(aK – a1),因为a2> a1。因此,可以说,包含我们的答案的子集在排序数组中将始终是连续的。
出于上述事实,为了解决问题,我们首先对数组进行排序,然后对第一个(N-K)个元素进行迭代,并且每次我们采用相距K个距离的元素之间的差,而最终的答案将是最小的。
C++
// C++ program to find minimum difference
// between max and min of all subset of K size
#include
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;
}
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.
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.
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
PHP
输出:
2
时间复杂度:O(n Log n)