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
- Only integer with positive value in positive negative value in array
- Maximum possible GCD for a pair of integers with product N
- Pair with given product | Set 1 (Find if any pair exists)
- Longest sequence of positive integers in an array
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Number of indices pair such that element pair sum from first Array is greater than second 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
- Number of arrays of size N whose elements are positive integers and sum is K
- Longest alternating subsequence in terms of positive and negative integers
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Count of elements to be multiplied with integers to make each pair of Array a perfect square
- Number of Subarrays with positive product
- Number of subsequences with positive product
- Length of longest subarray with positive product
- Minimum positive integer required to split the array equally
- Rearrange an array to minimize sum of product of consecutive pair elements
- Find pair with greatest product in array