Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Minimum Product Pair in List
In this problem, we need to find the pair of numbers from a list that produces the minimum product when multiplied together. Python provides several approaches to solve this using built-in functions like min() and modules like itertools.
Understanding the Problem
We need to find the pair of numbers whose multiplication is minimum compared to all other possible pairs. For example, given the list [1, 4, 3, 5, 1, 2, 8, 9], the pair (1, 1) has the minimum product of 1.
Method 1: Using List of Tuples
When we already have pairs as tuples, we can directly find the minimum product using list comprehension ?
Example
def find_min_product_tuple(pairs):
min_result = min([abs(a * b) for a, b in pairs])
return min_result
# List of tuples containing pairs
pairs = [(1, 6), (2, 4), (3, 8), (5, 1)]
print("Given pairs:", pairs)
print("Minimum product:", find_min_product_tuple(pairs))
Given pairs: [(1, 6), (2, 4), (3, 8), (5, 1)] Minimum product: 5
Complexity
Time complexity is O(n) where n is the number of pairs, as we iterate through the list once.
Method 2: Using itertools.combinations()
For a regular list, we generate all possible pairs using itertools.combinations() and find the pair with minimum product ?
Example
import itertools
def find_min_product_pair(numbers):
min_pair = min(itertools.combinations(numbers, 2), key=lambda pair: pair[0] * pair[1])
return min_pair
numbers = [2, 4, 3, 1, 5, 6, 7, 8, 9, 10]
print("Original list:", numbers)
result_pair = find_min_product_pair(numbers)
print("Minimum product pair:", result_pair)
print("Product:", result_pair[0] * result_pair[1])
Original list: [2, 4, 3, 1, 5, 6, 7, 8, 9, 10] Minimum product pair: (1, 2) Product: 2
Complexity
Time complexity is O(n²) where n is the size of the list, since we generate all possible combinations of pairs.
Method 3: Optimized Approach
For better performance, we can sort the list and find the two smallest elements ?
Example
def find_min_product_optimized(numbers):
sorted_nums = sorted(numbers)
return (sorted_nums[0], sorted_nums[1])
numbers = [2, 4, 3, 1, 5, 6, 7, 8, 9, 10]
print("Original list:", numbers)
result_pair = find_min_product_optimized(numbers)
print("Minimum product pair:", result_pair)
print("Product:", result_pair[0] * result_pair[1])
Original list: [2, 4, 3, 1, 5, 6, 7, 8, 9, 10] Minimum product pair: (1, 2) Product: 2
Comparison
| Method | Time Complexity | Best For |
|---|---|---|
| List of Tuples | O(n) | Pre-defined pairs |
| itertools.combinations() | O(n²) | Finding actual pair elements |
| Sorting Approach | O(n log n) | Large datasets |
Conclusion
Use the sorting approach for optimal performance with large datasets. Use itertools.combinations() when you need to find the actual pair elements. For pre-existing pairs, direct calculation works best.
