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 email@example.com. 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.
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.
- Minimum product of k integers in an array of positive Integers
- Find a pair with maximum product in array of Integers
- Maximum possible GCD for a pair of integers with product N
- Longest sequence of positive integers in an array
- Count pairs in Array whose product is a Kth power of any positive integer
- Count of index pairs in array whose range product is a positive integer
- Closest product pair in an array
- Construct an array from its pair-product
- Check if product of every pair exists in an array
- Find pair with greatest product in array
- Count of elements to be multiplied with integers to make each pair of Array a perfect square
- 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
- Minimum XOR of OR and AND of any pair in the Array
- Find minimum value to assign all array elements so that array product becomes greater
- Pair with given product | Set 1 (Find if any pair exists)
- 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