📌  相关文章
📜  从给定数组的四个元素组成的组中获得的最小和

📅  最后修改于: 2021-04-22 10:41:50             🧑  作者: Mango

给定N个整数的数组arr [] ,其中N%4 = 0 ,任务是将这些整数分成四组,这样,当取所有组中最多两个元素的总和时,则是最小的。打印最小金额。

例子:

方法:为了使总和最小,数组中最多四个元素必须在同一组中,因为无论它们属于哪个组,总和中肯定会包含最多两个元素,但接下来的两个最大元素可以如果他们是这个团体的一部分,就可以避免。以相同的方式进行分组将使可能的总和最小。因此,以降序对数组进行排序,并从第一个元素开始,将四个连续元素组成一组。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the minimum required sum
int minSum(int arr[], int n)
{
  
    // To store the required sum
    int sum = 0;
  
    // Sort the array in descending order
    sort(arr, arr + n, greater());
    for (int i = 0; i < n; i++) {
  
        // The indices which give 0 or 1 as
        // the remainder when divided by 4
        // will be the maximum two
        // elements of the group
        if (i % 4 < 2)
            sum = sum + arr[i];
    }
  
    return sum;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 1, 10, 2, 2, 2, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << minSum(arr, n);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG 
{
  
// Function to return the minimum required sum
static int minSum(Integer arr[], int n)
{
  
    // To store the required sum
    int sum = 0;
  
    // Sort the array in descending order
    Arrays.sort(arr, Collections.reverseOrder());
    for (int i = 0; i < n; i++) 
    {
  
        // The indices which give 0 or 1 as
        // the remainder when divided by 4
        // will be the maximum two
        // elements of the group
        if (i % 4 < 2)
            sum = sum + arr[i];
    }
  
    return sum;
}
  
// Driver code
public static void main(String[] args)
{
    Integer []arr = { 1, 1, 10, 2, 2, 2, 1 };
    int n = arr.length;
  
    System.out.println(minSum(arr, n));
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach 
  
# Function to return the minimum required sum 
def minSum(arr, n) : 
  
    # To store the required sum 
    sum = 0; 
  
    # Sort the array in descending order 
    arr.sort(reverse = True)
      
    for i in range(n) :
  
        # The indices which give 0 or 1 as 
        # the remainder when divided by 4 
        # will be the maximum two 
        # elements of the group 
        if (i % 4 < 2) :
            sum += arr[i]; 
      
    return sum; 
  
# Driver code 
if __name__ == "__main__" :
    arr = [ 1, 1, 10, 2, 2, 2, 1 ]; 
    n = len(arr);
    print(minSum(arr, n)); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;             
  
class GFG 
{
  
// Function to return the minimum required sum
static int minSum(int []arr, int n)
{
  
    // To store the required sum
    int sum = 0;
  
    // Sort the array in descending order
    Array.Sort(arr);
    Array.Reverse(arr);
    for (int i = 0; i < n; i++) 
    {
  
        // The indices which give 0 or 1 as
        // the remainder when divided by 4
        // will be the maximum two
        // elements of the group
        if (i % 4 < 2)
            sum = sum + arr[i];
    }
    return sum;
}
  
// Driver code
public static void Main(String[] args)
{
    int []arr = { 1, 1, 10, 2, 2, 2, 1 };
    int n = arr.Length;
  
    Console.WriteLine(minSum(arr, n));
}
}
  
// This code is contributed by 29AjayKumar


输出:
14