A basic approach using two nested loops
A simple approach will be to run two nested loops to generate all possible pairs of elements and keep track of the minimum product.
Algorithm:
- Initialize a variable ‘minProduct’ as maximum possible value.
- Traverse the given array using two nested loops:
a. For each pair of elements, calculate their product and update ‘minProduct’ if it is lesser than the current value. - Return the final value of ‘minProduct’.
Below is the implementation of the approach:
C++
Java
// Java code for the approach public class GFG { // Function to print the minimum product of any // two numbers in the array static int minProduct( int [] arr, int n) { // Initializing minimum product int minProd = Integer.MAX_VALUE; // Loop to generate all possible pairs of elements for ( int i = 0 ; i < n - 1 ; i++) { for ( int j = i + 1 ; j < n; j++) { // Updating minimum product minProd = Math.min(minProd, arr[i] * arr[j]); } } // Return the minimum product return minProd; } // Driver code public static void main(String[] args) { int [] arr = { 11 , 8 , 5 , 7 , 5 , 100 }; int n = arr.length; // Function call int minProd = minProduct(arr, n); System.out.println(minProd); } } |
Python3
# Function to print the minimum product of any # two numbers in the array def minProduct(arr, n): # Initializing minimum product minProd = float ( 'inf' ) # Loop to generate all possible pairs of elements for i in range (n - 1 ): for j in range (i + 1 , n): # Updating minimum product minProd = min (minProd, arr[i] * arr[j]) # Return the minimum product return minProd # Driver code if __name__ = = "__main__" : arr = [ 11 , 8 , 5 , 7 , 5 , 100 ] n = len (arr) # Function call minProd = minProduct(arr, n) print (minProd) # This code is contributed by shivamgupta310570 |
C#
// C# code for the approach using System; public class GFG { // Function to print the minimum product of any // two numbers in the array static int minProduct( int [] arr, int n) { // Initializing minimum product int minProd = Int32.MaxValue; // Loop to generate all possible pairs of elements for ( int i = 0; i < n - 1; i++) { for ( int j = i + 1; j < n; j++) { // Updating minimum product minProd = Math.Min(minProd, arr[i] * arr[j]); } } // Return the minimum product return minProd; } // Driver code public static void Main() { int [] arr = {11, 8, 5, 7, 5, 100}; int n = arr.Length; // Function call int minProd = minProduct(arr, n); Console.WriteLine(minProd); } } // This code is contributed by Vaibhav Nandan |
Javascript
Output
25
Time Complexity: O( n * 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.