An optimized approach using sorting
An optimized approach will be to first sort the given array and print the product of the first two numbers, sorting will take O(n log n) and the answer will be a[0] * a[1]
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to calculate minimum product // of pair int printMinimumProduct( int arr[], int n) { //Sort the array sort(arr,arr+n); // Returning the product of first two numbers return arr[0] * arr[1]; } // Driver program to test above function int main() { int a[] = { 11, 8 , 5 , 7 , 5 , 100 }; int n = sizeof (a) / sizeof (a[0]); cout << printMinimumProduct(a,n); return 0; } // This code is contributed by Pushpesh Raj |
Java
// java program for the above approach import java.util.*; class GFG { // Function to calculate minimum product // of pair static int printMinimumProduct( int arr[], int n) { // Sort the array Arrays.sort(arr); // Returning the product of first two numbers return arr[ 0 ]*arr[ 1 ]; } // Driver program to test above function public static void main (String[] args) { int a[]={ 11 , 8 , 5 , 7 , 5 , 100 }; int n=a.length; System.out.println(printMinimumProduct(a,n)); } } // This code is contributed by nmkiniqw7b. |
Python3
# Python program for the above approach # Function to calculate minimum product # of pair def printMinimumProduct( list ,n): # Sort the list list .sort() # Returning the product of first two numbers return list [ 0 ] * list [ 1 ] # Driver code a = [ 11 , 8 , 5 , 7 , 5 , 100 ] n = len (a) print (printMinimumProduct(a,n)) # This code is contributed by nmkiniqw7b. |
C#
// C# program for the above approach using System; class GFG { //function to calculate minimum product of pair static int printMinimumProduct( int []arr, int n) { // first sort the array Array.Sort(arr); // returning the product of first two numbers return arr[0] * arr[1]; } // driver program static void Main() { int []a = { 11, 8 , 5 , 7 ,5 , 100 }; int n = a.Length; Console.WriteLine(printMinimumProduct(a, n)); } } |
Javascript
// Javascript program for the above approach // Function to calculate minimum product // of pair function printMinimumProduct(arr, n) { // Sort the array arr.sort( function (a, b){ return a - b}); // Returning the product of first two numbers return arr[0] * arr[1]; } // Driver program to test above function let a = [ 11, 8 , 5 , 7 , 5 , 100 ]; let n = a.Length; console.log(printMinimumProduct(a,n)); // This code is contributed by Aman Kumar |
25
Time Complexity: O( n * log(n))
Auxiliary Space: O(1)
Minimum product pair an array of positive Integers
Given an array of positive integers. We are required to write a program to print the minimum product of any two numbers of the given array.
Examples:
Input: 11 8 5 7 5 100
Output: 25
Explanation: The minimum product of any two numbers will be 5 * 5 = 25.Input: 198 76 544 123 154 675
Output: 7448
Explanation: The minimum product of any two numbers will be 76 * 123 = 7448.