📌  相关文章
📜  给定数组的成对元素的最小乘积之和

📅  最后修改于: 2021-04-28 17:26:16             🧑  作者: Mango

给定其中元素N为偶数的数组arr [] 。任务是形成N / 2对,以使这些对中的元素乘积之和最小。
例子

方法:

  • 对给定数组arr []中的元素进行排序。
  • 先对两个元素,然后再对两个元素进行配对,依此类推。
  • 计算形成的相应对的乘积之和。

下面是上述方法的实现:

CPP
// C++ program to find the minimum
// product of sum of pair of element
// in array arr[]
#include "bits/stdc++.h"
using namespace std;
 
// Function to find the minimum
// product
int minimumProduct(int* arr, int n)
{
 
    // Sort the array using STL
    // sort() function
    sort(arr, arr + n);
 
    // Intialise product to 1
    int product = 1;
 
    for (int i = 0; i < n; i += 2) {
 
        // Find product of sum of
        // all pairs
        product *= (arr[i] + arr[i + 1]);
    }
 
    // Return the product
    return product;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 6, 3, 1, 7, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call to find product
    cout << minimumProduct(arr, n) << endl;
    return 0;
}


Java
// Java program to find the minimum
// product of sum of pair of element
// in array arr[]
import java.util.*;
 
class GFG{
  
// Function to find the minimum
// product
static int minimumProduct(int[] arr, int n)
{
  
    // Sort the array using STL
    // sort() function
    Arrays.sort(arr);
  
    // Intialise product to 1
    int product = 1;
  
    for (int i = 0; i < n; i += 2) {
  
        // Find product of sum of
        // all pairs
        product *= (arr[i] + arr[i + 1]);
    }
  
    // Return the product
    return product;
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 6, 3, 1, 7, 8 };
    int n = arr.length;
  
    // Function call to find product
    System.out.print(minimumProduct(arr, n) +"\n");
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 program to find the minimum
# product of sum of pair of element
# in array arr[]
 
# Function to find the minimum
# product
def minimumProduct(arr, n):
 
    # Sort the array using STL
    # sort() function
    arr = sorted(arr)
 
    # Intialise product to 1
    product = 1
 
    for i in range(0, n, 2):
 
        # Find product of sum of
        # all pairs
        product *= (arr[i] + arr[i + 1])
 
    # Return the product
    return product
 
# Driver code
 
arr = [1, 6, 3, 1, 7, 8]
n = len(arr)
 
# Function call to find product
print(minimumProduct(arr, n))
 
# This code is contributed by mohit kumar 29


C#
// C# program to find the minimum
// product of sum of pair of element
// in array arr[]
using System;
 
class GFG {
 
    // Function to find the minimum
    // product
    static int minimumProduct(int[] arr, int n)
    {
     
        // Sort the array
        // sort() function
        Array.Sort(arr);
     
        // Intialise product to 1
        int product = 1;
     
        for (int i = 0; i < n; i += 2) {
     
            // Find product of sum of
            // all pairs
            product *= (arr[i] + arr[i + 1]);
        }
     
        // Return the product
        return product;
    }
     
    // Driver code
    static void Main()
    {
        int[] arr = new int[] { 1, 6, 3, 1, 7, 8 };
        int n = arr.Length;
     
        // Function call to find product
        Console.Write(minimumProduct(arr, n));
    }
}
 
// This code is contributed by shubhamsingh10


Javascript


输出:
270

时间复杂度: O(N * log N)