Open In App

Python – Pair with Maximum product

Last Updated : 22 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, we need to find the specific problem of getting the pair which yields the maximum product, this can be computed by getting initial two elements after sorting. But in some case, we don’t with to change the ordering of list and perform some operation in the similar list without using extra space. Let’s discuss certain ways in which this can be performed. 

Method #1 : Using list comprehension + max() + combination() + lambda This particular task can be performed using the combination of above functions in which we use list comprehension to bind all the functionalities and max function to get the maximum prod, combination function finds all prods internally and lambda function is used to compute the product. 

Python3




# Python3 code to demonstrate
# Pair with Maximum product
# using list comprehension + max() + combinations() + lambda
from itertools import combinations
 
# initializing list
test_list = [3, 4, 1, 7, 9, 1]
 
# printing original list
print("The original list : " + str(test_list))
 
# using list comprehension + max() + combinations() + lambda
# Pair with Maximum product
res = max(combinations(test_list, 2), key = lambda sub: abs(sub[0] * sub[1]))
 
# print result
print("The maximum product pair is : " + str(res))


Output : 

The original list : [3, 4, 1, 7, 9, 1]
The maximum product pair is : (7, 9)

Time complexity: O(n*n), where n is the length of the test_list. The list comprehension + max() + combination() + lambda takes O(n*n) time
Auxiliary Space: O(n), extra space of size n is required

  Method #2 : Using list comprehension + nlargest() + combination() + lambda This method has potential of not only finding a single maximum but also k maximum product pairs if required and uses nlargest function instead of max function to achieve this functionality. 

Python3




# Python3 code to demonstrate
# Pair with Maximum product
# using list comprehension + nlargest() + combinations() + lambda
from itertools import combinations
from heapq import nlargest
 
# initializing list
test_list = [3, 4, 1, 7, 9, 8]
 
# printing original list
print("The original list : " + str(test_list))
 
# using list comprehension + nlargest() + combinations() + lambda
# Pair with Maximum product
# computes 2 max product pair
res = nlargest(2, combinations(test_list, 2), key = lambda sub: abs(sub[0] * sub[1]))
 
# print result
print("The maximum product pair is : " + str(res))


Output : 

The original list : [3, 4, 1, 7, 9, 8]
The maximum product pair is : [(9, 8), (7, 9)]

Time Complexity: O(n), where n is the length of the input list. This is because we’re using list comprehension + nlargest() + combination() + lambda which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list

Method #3 : Using max() and zip()

Python3




# Python3 code to demonstrate
# Pair with Maximum product
# using max() and zip()
   
# initializing list
test_list = [3, 4, 1, 7, 9, 8]
   
# printing original list
print("The original list : " + str(test_list))
 
# using max() and zip()
# Pair with Maximum product
res = max(zip(test_list, test_list[1:]), key = lambda x: x[0] * x[1])
 
# print result
print("The maximum product pair is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list : [3, 4, 1, 7, 9, 8]
The maximum product pair is : (9, 8)

This method uses the zip() function to pair up adjacent elements in the input list, and the max() function to find the pair with the maximum product. The lambda function is used to extract the product of each pair.

Time Complexity: O(n)
Space Complexity: O(n)

Method #4 : Using numpy:

  1. Convert the input list to a NumPy array using np.array().
  2. Generate a 2D array of all possible pairs of the input array using np.meshgrid().
  3. Reshape the 2D array into a 1D array of pairs using np.reshape().
  4. Filter out the pairs where both elements are equal using comb[:, 0] != comb[:, 1].
  5. Calculate the product of each pair using np.prod() along the second axis.
  6. Find the index of the maximum absolute product using np.argmax().
  7. Retrieve the pair with the maximum product from the original array using the index found in step 6.
  8. Convert the result to a tuple using tuple().

Python3




import numpy as np
 
# initializing list
test_list = [3, 4, 1, 7, 9, 1]
 
# printing original list
print("The original list : " + str(test_list))
 
# using NumPy to find maximum product
arr = np.array(test_list)
comb = np.array(np.meshgrid(arr, arr)).T.reshape(-1, 2)
comb = comb[comb[:, 0] != comb[:, 1]]
prod = np.prod(comb, axis=1)
max_idx = np.argmax(np.abs(prod))
res = tuple(comb[max_idx])
 
# print result
print("The maximum product pair is : " + str(res))
#This code is contributed by pushpa


Output:

The original list : [3, 4, 1, 7, 9, 1]
The maximum product pair is : (7, 9)

The time complexity : O(n^2) due to the use of np.meshgrid(), which generates a full 2D array of all possible pairs. 

The space complexity : O(n^2) due to the storage of the 2D array.

Method #5: Using itertools.product() and max()

This method involves using itertools.product() to find all possible pairs of elements in the list, and then finding the pair with the maximum product using the max() function.

step-by-step approach:

  1. Import itertools
     
  2. Find all possible pairs of elements in the list using itertools.product()
     
  3. Filter out the pairs where both elements are the same using a lambda function and list comprehension
    Find the pair with the maximum product using the max() function and a lambda function
     
  4. Print the result

Python3




import itertools
 
# initializing list
test_list = [3, 4, 1, 7, 9, 1]
 
# printing original list
print("The original list : " + str(test_list))
 
# using itertools.product() and max() to find maximum product pair
comb = list(itertools.product(test_list, repeat=2))
comb = [pair for pair in comb if pair[0] != pair[1]]
max_pair = max(comb, key=lambda pair: pair[0]*pair[1])
 
# print result
print("The maximum product pair is : " + str(max_pair))


Output

The original list : [3, 4, 1, 7, 9, 1]
The maximum product pair is : (7, 9)

Time complexity: O(n^2), where n is the length of the list
Auxiliary space: O(n^2) to store all the possible pairs



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads