给定N个整数的数组arr [] ,任务是将数组拆分为两个子集,以使第一子集的最大值与第二子集的最小值之间的绝对差最小。
例子:
Input: arr[] = {3, 1, 2, 6, 4}
Output: 1
Explanation:
Splitting the given array in two subsets, A = [1, 2, 4], B = [3, 6]. Difference of maximum of first set is 4 and minimum of second set is 3 and their difference is 1.
Input: arr[] = {2, 1, 3, 2, 4, 3}
Output: 0
Explanation:
Splitting the given array in two subsets, A = [1, 2, 2, 3], B = [3, 4]. Difference of maximum of first set is 3 and minimum of second set is 3 and their difference is 0.
方法:为了解决上述问题,我们必须找到两个整数,使得m和n使得第一个集合的最大值为m ,第二个集合的最小值为n 。想法是对给定的数组升序进行排序,对数组进行排序后,连续元素之间的最小差异就是将数组元素划分为子集后所需的最小差异。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to split the array
int splitArray(int arr[], int N)
{
// Sort the array in increasing order
sort(arr, arr + N);
int result = INT_MAX;
// Calculating the max difference
// between consecutive elements
for (int i = 1; i < N; i++) {
result = min(result,
arr[i] - arr[i - 1]);
}
// Return the final minimum difference
return result;
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 3, 1, 2, 6, 4 };
// Size of array
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
cout << splitArray(arr, N);
return 0;
}
Java
// java program for the above approach
import java.util.*;
class GFG{
// Function to split the array
static int splitArray(int arr[], int N)
{
// Sort the array in increasing order
Arrays.sort(arr);
int result = Integer.MAX_VALUE;
// Calculating the max difference
// between consecutive elements
for (int i = 1; i < N; i++)
{
result = Math.min(result,
arr[i] - arr[i - 1]);
}
// Return the final minimum difference
return result;
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = { 3, 1, 2, 6, 4 };
// Size of array
int N = arr.length;
// Function Call
System.out.print(splitArray(arr, N));
}
}
// This code is contributed by shivanisinghss2110
Python3
# Python3 program for the above approach
# Function to split the array
def splitArray(arr, N):
# Sort the array in increasing
# order
arr = sorted(arr)
result = 10 ** 9
# Calculating the max difference
# between consecutive elements
for i in range(1, N):
result = min(result, arr[i] - arr[i - 1])
# Return the final minimum difference
return result
# Driver Code
if __name__ == '__main__':
# Given array arr[]
arr = [ 3, 1, 2, 6, 4 ]
# Size of array
N = len(arr)
# Function Call
print(splitArray(arr, N))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to split the array
static int splitArray(int []arr, int N)
{
// Sort the array in increasing order
Array.Sort(arr);
int result = Int32.MaxValue;
// Calculating the max difference
// between consecutive elements
for (int i = 1; i < N; i++)
{
result = Math.Min(result,
arr[i] - arr[i - 1]);
}
// Return the final minimum difference
return result;
}
// Driver Code
public static void Main()
{
// Given array arr[]
int []arr = { 3, 1, 2, 6, 4 };
// Size of array
int N = arr.Length;
// Function Call
Console.Write(splitArray(arr, N));
}
}
// This code is contributed by Code_Mech
Javascript
1
时间复杂度: O(N * log N)