Python - Minimum Product Pair in List


In the given problem statement, we have to find the minimum product pair in the given list and list of tuples. So we will use Python to implement the code.

Understanding the Problem

The problem at hand is to find the minimum product from the given list and create a program in Python. Or we can say that we have to find the pair of numbers whose multiplication is minimum as compared to the other pairs in the list. There are many ways to solve this problem. So we will use list and list of tuples to showcase this problem statement. Let’s understand this problem with the help of an example −

Let’s say we have a list as [1, 4, 3, 5, 1, 2, 8, 9], within this list we can see there are many possible pairs but we have to find the pair with the minimum product and show it as an Output. In this example there is (1,1) pair in which the product of these two items is 1, which is less in all the possible combinations.

Using List of Tuple or Tuple List

The list of tuples or tuple list, is a data structure in the Python programming language. Tuple list contains multiple tuples as its items. Means every element inside the list is a tuple.

To understand the logic for this program, we will use the ‘min’ function of Python to get the minimum value from the given numbers. And then we will use the abs function to give the absolute value of the number. Inside the abs function we will calculate the product of the numbers a and b. And store the result in a separate variable.

Algorithm

  • Step 1 − As we have to find the minimum product pair in the given list of tuples so first we will create a function called list_min_val and inside this function we will pass a parameter called numbers which is a list of tuples type.

  • Step 2 − After defining the function we will use a variable to store the minimum product of the pair and give it a name as min_result. And initialize its value by calculating the product and min or that product with the help of min and ans functions.

  • Step 3 − Now we will return the min_result as a result. And then we will define the list tuple as input and print the values of the result.

Example

#Function to find the minimum,  product in the given list  
def list_min_val(numbers):
   min_result = min([abs(a * b) for a, b in numbers] )
   return min_result
numbers = [(1, 6), (2, 4), (3, 8), (5, 1)]  
print("\nMinimum product from the pairs of the given tuple list:")
print(list_min_val(numbers))

Output

Minimum product from the pairs of the given tuple list:
5

Complexity

The time taken for finding the minimum product in the given list is O(n), here n is the size of the given list of tuples. As the code only does a single iteration of numbers to calculate the product of the numbers.

Using the List

In this approach we will use the list data structure of Python. And we will use itertools to create the iterators for the looping purpose.

Algorithm

  • Step 1 − As we are using itertools module of python so first we will import it as itts. This module will help us to create iterators for looping purposes.

  • Step 2 − After importing the itertools we will create a function called list_min_pair and inside the function we will pass the list called numbers.

  • Step 3 − Inside the above function we will use a variable to store the minimum product result. And initialize its value using the min, lambda function and itts.combinations. With the help of the lambda function we will calculate the product of two numbers. And the ‘itts.combinations’ function will be used to find the combinations of the numbers with minimum product value.

  • Step 4 − Now we will define a numbers list and print the Output with the minimum product to show on the console.

Example

# Import itertools to create iterators for efficient looping
import itertools as itts

# Function to get the minimum product pair
def list_min_pair(numbers):
   min_result = min(itts.combinations(numbers, 2), key = lambda sub: sub[0] * sub[1])
   return min_result

numbers = [2, 4, 3, 1, 5, 6, 7, 8, 9, 10]  
print("\nThe actual list:")
print((numbers))
print("\nPair of minimum product from the given input list:")
print(list_min_pair(numbers))

Output

The actual list:
[2, 4, 3, 1, 5, 6, 7, 8, 9, 10]

Pair of minimum product from the given input list:
(2, 1)

Complexity

The time complexity for getting the minimum product pair using the list in python is (n^2), here n is the size of the given input list. The reason for this complexity is that we are making combinations of the elements of the list so for making these combinations we are required n^2 time. So the resultant time complexity is O(n^2).

Conclusion

So we have seen different approaches for finding the minimum product pair in the given list. We have seen the usage of itertools module, min function, lambda function and also abs function. So with the help of these codes we can learn the usage of these in-built functions in Python.

Updated on: 16-Oct-2023

43 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements