给定一个偶数个元素的数组,请使用这些数组元素形成2个组,以使总和最高的组与总和最小的组之间的差最大。
注意:元素只能是一个组的一部分,并且必须至少是一个组的一部分。
例子:
Input : arr[] = {1, 4, 9, 6}
Output : 10
Groups formed will be (1, 4) and (6, 9),
the difference between highest sum group
(6, 9) i.e 15 and lowest sum group (1, 4)
i.e 5 is 10.
Input : arr[] = {6, 7, 1, 11}
Output : 11
Groups formed will be (1, 6) and (7, 11),
the difference between highest sum group
(7, 11) i.e 18 and lowest sum group (1, 6)
i.e 7 is 11.
简单方法:我们可以通过进行所有可能的组合并检查最高和最低组之间的每组组合差异来解决此问题。总共将形成n *(n-1)/ 2个这样的基团(nC2)。
时间复杂度:O(n ^ 3),因为将花费O(n ^ 2)来生成组并检查每个组n次迭代,因此总体上需要O(n ^ 3)时间。
高效方法:我们可以使用贪婪方法。对整个数组进行排序,我们的结果是最后两个元素的总和减去前两个元素的总和。
C++
// CPP program to find minimum difference
// between groups of highest and lowest
// sums.
#include
#define ll long long int
using namespace std;
ll CalculateMax(ll arr[], int n)
{
// Sorting the whole array.
sort(arr, arr + n);
int min_sum = arr[0] + arr[1];
int max_sum = arr[n-1] + arr[n-2];
return abs(max_sum - min_sum);
}
// Driver code
int main()
{
ll arr[] = { 6, 7, 1, 11 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << CalculateMax(arr, n) << endl;
return 0;
}
Java
// Java program to find minimum difference
// between groups of highest and lowest
// sums.
import java.util.Arrays;
import java.io.*;
class GFG {
static int CalculateMax(int arr[], int n)
{
// Sorting the whole array.
Arrays.sort(arr);
int min_sum = arr[0] + arr[1];
int max_sum = arr[n-1] + arr[n-2];
return (Math.abs(max_sum - min_sum));
}
// Driver code
public static void main (String[] args) {
int arr[] = { 6, 7, 1, 11 };
int n = arr.length;
System.out.println (CalculateMax(arr, n));
}
}
Python3
# Python 3 program to find minimum difference
# between groups of highest and lowest
def CalculateMax(arr, n):
# Sorting the whole array.
arr.sort()
min_sum = arr[0] + arr[1]
max_sum = arr[n - 1] + arr[n - 2]
return abs(max_sum - min_sum)
# Driver code
arr = [6, 7, 1, 11]
n = len(arr)
print(CalculateMax(arr, n))
# This code is contributed
# by Shrikant13
C#
// C# program to find minimum difference
// between groups of highest and lowest
// sums.
using System;
public class GFG{
static int CalculateMax(int []arr, int n)
{
// Sorting the whole array.
Array.Sort(arr);
int min_sum = arr[0] + arr[1];
int max_sum = arr[n-1] + arr[n-2];
return (Math.Abs(max_sum - min_sum));
}
// Driver code
static public void Main (){
int []arr = { 6, 7, 1, 11 };
int n = arr.Length;
Console.WriteLine(CalculateMax(arr, n));
}
//This code is contributed by Sachin.
}
PHP
输出:
11
时间复杂度: O(n * log n)
进一步优化:
不用排序,我们可以在线性时间中找到最大2个和最小2个,并将时间复杂度降低到O(n)。