📌  相关文章
📜  数组中对的最小值的最大和

📅  最后修改于: 2021-04-27 21:10:39             🧑  作者: Mango

给定的阵列ARR N个整数,其中N是偶数的[]中,任务是组中的对的数组元素(X1,Y1),(X2,Y2),(X3,Y3),…,使得总和分钟( X1,Y1)+ min(X2,Y2)+ min(X3,Y3)+…最大。

例子:

方法:无论成对如何形成,数组中的最大元素将始终被忽略,因为它将成为放入它的每一对中的最大元素。除非它与最大元素配对,否则第二个最大元素也是如此。因此,为了使总和最大化,一种最佳方法是对数组进行排序,并从最大元素开始按顺序开始配对。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the maximum
// required sum of the pairs
int maxSum(int a[], int n)
{
  
    // Sort the array
    sort(a, a + n);
  
    // To store the sum
    int sum = 0;
  
    // Start making pairs of every two
    // consecutive elements as n is even
    for (int i = 0; i < n - 1; i += 2) {
  
        // Minimum element of the current pair
        sum += a[i];
    }
  
    // Return the maximum possible sum
    return sum;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 3, 2, 1, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << maxSum(arr, n);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.Arrays;
  
class GFG
{
      
// Function to return the maximum
// required sum of the pairs
static int maxSum(int a[], int n)
{
  
    // Sort the array
    Arrays.sort(a);
  
    // To store the sum
    int sum = 0;
  
    // Start making pairs of every two
    // consecutive elements as n is even
    for (int i = 0; i < n - 1; i += 2)
    {
  
        // Minimum element of the current pair
        sum += a[i];
    }
  
    // Return the maximum possible sum
    return sum;
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 3, 2, 1, 4, 5 };
    int n = arr.length;
  
    System.out.println(maxSum(arr, n));
}
}
  
// This code is contributed by Code_Mech


Python3
# Python3 implementation of the approach 
  
# Function to return the maximum 
# required sum of the pairs 
def maxSum(a, n) :
  
    # Sort the array 
    a.sort(); 
  
    # To store the sum 
    sum = 0; 
  
    # Start making pairs of every two 
    # consecutive elements as n is even 
    for i in range(0, n - 1, 2) : 
  
        # Minimum element of the current pair 
        sum += a[i]; 
  
    # Return the maximum possible sum 
    return sum; 
  
# Driver code 
if __name__ == "__main__" : 
  
    arr = [ 1, 3, 2, 1, 4, 5 ]; 
    n = len(arr); 
  
    print(maxSum(arr, n));
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
      
class GFG
{
      
// Function to return the maximum
// required sum of the pairs
static int maxSum(int []a, int n)
{
  
    // Sort the array
    Array.Sort(a);
  
    // To store the sum
    int sum = 0;
  
    // Start making pairs of every two
    // consecutive elements as n is even
    for (int i = 0; i < n - 1; i += 2)
    {
  
        // Minimum element of the current pair
        sum += a[i];
    }
  
    // Return the maximum possible sum
    return sum;
}
  
// Driver code
public static void Main(String[] args)
{
    int []arr = { 1, 3, 2, 1, 4, 5 };
    int n = arr.Length;
  
    Console.WriteLine(maxSum(arr, n));
}
}
  
// This code is contributed by 29AjayKumar


输出:
7