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.
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.
Simple Approach : A simple approach will be to run two nested loops to generate all possible pair of elements and keep track of the minimum product.
Time Complexity: O( n * n)
Auxiliary Space: O( 1 )
Better Approach: An efficient approach will be to first sort the given array and print the product of first two numbers, sorting will take O(n log n). Answer will be then a * a
Time Complexity: O( n * log(n))
Auxiliary Space: O( 1 )
Best Approach: The idea is linearly traverse given array and keep track of minimum two elements. Finally return product of two minimum elements.
Below is the implementation of above approach.
Time Complexity: O(n)
Auxiliary Space: O(1)
This article is contributed by Raja Vikramaditya. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Minimum product of k integers in an array of positive Integers
- Find a pair with maximum product in array of Integers
- Longest sequence of positive integers in an array
- Closest product pair in an array
- Find pair with greatest product in array
- Check if product of every pair exists in an array
- Rearrange an array to minimize sum of product of consecutive pair elements
- Minimum positive integer required to split the array equally
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Pair with given product | Set 1 (Find if any pair exists)
- Find minimum value to assign all array elements so that array product becomes greater
- Minimum increments to convert to an array of consecutive integers
- Minimum array size after repeated replacement of even sum pair with sum
- Minimum pair sum operations to make array each element divisible by 4
- Product of maximum in first array and minimum in second