Given an array arr[] of even number of element N in it. The task is to form N/2 pairs such that sum of product of elements in those pairs is minimum.
Examples
Input: arr[] = { 1, 6, 3, 1, 7, 8 }
Output: 270
Explanation:
The pair formed are {1, 1}, {3, 6}, {7, 8}
Product of sum of these pairs = 2 * 9 * 15 = 270Input: arr[] = {2, 3, 90, 12}
Output: 510
Explanation:
Pairs should be created in this way {2, 3}, {12, 90}
Product of sum of these pairs = 5*112 = 510
Approach:
- Sort the elements in the given array arr[].
- Make pairs of first two-element, then next two-element and so on.
- Calculate the sum of product of corresponding pairs formed.
Below is the implementation of the above approach:
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 |
270
Time Complexity: O(N*log N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Sum of product of all pairs of array elements
- Find minimum value to assign all array elements so that array product becomes greater
- Minimum steps to make sum and the product of all elements of array non-zero
- Product of minimum edge weight between all pairs of a Tree
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Find pairs of elements from two different arrays whose product is a perfect square
- Product of all the pairs from the given array
- Number of pairs in an array having sum equal to product
- Number of pairs in an array such that product is greater than sum
- Count pairs in a sorted array whose product is less than k
- Count of pairs in an array whose product is a perfect square
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Count of pairs in an array such that the highest power of 2 that divides their product is 1
- Count pairs in Array whose product is a Kth power of any positive integer
- Minimum sum by choosing minimum of pairs from array
- Count of index pairs in array whose range product is a positive integer
- Find an index such that difference between product of elements before and after it is minimum
- Product of all Subsequences of size K except the minimum and maximum Elements
- Minimum LCM of all pairs in a given array
- Product of maximum in first array and minimum in second
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

