📌  相关文章
📜  大小为 2 的组之间的最小差异

📅  最后修改于: 2021-10-26 05:42:33             🧑  作者: Mango

给定偶数个元素的数组,使用这些数组元素组成 2 组,使得总和最高的组与总和最低的组之间的差异最小。
注意:一个元素只能是一个组的一部分,并且它必须至少是 1 个组的一部分。
例子:

Input : arr[] = {2, 6, 4, 3}
Output : 1
Groups formed will be (2, 6) and (4, 3), 
the difference between highest sum group
(2, 6) i.e 8 and lowest sum group (3, 4)
i.e 7 is 1.

Input : arr[] = {11, 4, 3, 5, 7, 1}
Output : 3
Groups formed will be (1, 11), (4, 5) and
(3, 7), the difference between highest 
sum group (1, 11) i.e 12 and lowest sum 
group (4, 5) i.e 9 is 3.

简单方法:一种简单的方法是尝试针对数组元素的所有组合,并检查总和最高的组和总和最低的组之间的每组组合差异。将形成总共 n*(n-1)/2 个这样的组 (nC2)。
时间复杂度:O(n^3) 生成组需要 n^2 次迭代并检查每个组需要 n 次迭代,因此在最坏的情况下需要 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 calculate(ll a[], ll n)
{
    // Sorting the whole array.
    sort(a, a + n);
 
    // Generating sum groups.
    vector s;
    for (int i = 0, j = n - 1; i < j; i++, j--)
       s.push_back(a[i] + a[j]);
 
    ll mini = *min_element(s.begin(), s.end());
    ll maxi = *max_element(s.begin(), s.end());
 
    return abs(maxi - mini);
}
 
int main()
{
    ll a[] = { 2, 6, 4, 3 };
    int n = sizeof(a) / (sizeof(a[0]));
    cout << calculate(a, n) << endl;
    return 0;
}


Java
// Java program to find minimum
// difference between groups of
// highest and lowest sums.
import java.util.Arrays;
import java.util.Collections;
import java.util.Vector;
 
 
class GFG {
 
static long calculate(long a[], int n)
{
    // Sorting the whole array.
    Arrays.sort(a);
    int i,j;
     
    // Generating sum groups.
    Vector s = new Vector<>();
    for (i = 0, j = n - 1; i < j; i++, j--)
        s.add((a[i] + a[j]));
         
    long mini = Collections.min(s);
    long maxi = Collections.max(s);
    return Math.abs(maxi - mini);
}
 
// Driver code
public static void main(String[] args)
{
    long a[] = { 2, 6, 4, 3 };
    int n = a.length;
    System.out.println(calculate(a, n));
    }
}
// This code is contributed by 29AjayKumar


Python3
# Python3 program to find minimum
# difference between groups of
# highest and lowest sums.
def calculate(a, n):
     
    # Sorting the whole array.
    a.sort();
 
    # Generating sum groups.
    s = [];
    i = 0;
    j = n - 1;
    while(i < j):
        s.append((a[i] + a[j]));
        i += 1;
        j -= 1;
 
    mini = min(s);
    maxi = max(s);
 
    return abs(maxi - mini);
 
# Driver Code
a = [ 2, 6, 4, 3 ];
n = len(a);
print(calculate(a, n));
 
# This is contributed by mits


C#
// C# program to find minimum
// difference between groups of
// highest and lowest sums.
using System;
using System.Linq;
using System.Collections.Generic;
 
class GFG
{
 
    static long calculate(long []a, int n)
    {
        // Sorting the whole array.
        Array.Sort(a);
        int i, j;
 
        // Generating sum groups.
        List s = new List();
        for (i = 0, j = n - 1; i < j; i++, j--)
            s.Add((a[i] + a[j]));
 
        long mini = s.Min();
        long maxi = s.Max();
        return Math.Abs(maxi - mini);
    }
 
    // Driver code
    public static void Main()
    {
        long []a = { 2, 6, 4, 3 };
        int n = a.Length;
        Console.WriteLine(calculate(a, n));
    }
}
 
//This code is contributed by Rajput-Ji


PHP


Javascript


输出:
1

时间复杂度: O (n * log n)
提问: Inmobi
参考:https://www.hackerearth.com/problem/algorithm/project-team/