Python – Maximum product using K elements
Sometimes, while working with Python lists, we can have a problem in which we need to maximize certain numbers. There can be many conditions of maximizing. Example of that is maximizing product by using K elements. Lets discuss certain ways in which this can be performed.
Method #1 : Using max() + sort() + list comprehension The combination of above functions can be used to solve this problem. In this, we perform sort and then extract maximum by using the best initial or rear elements using max().
Python3
test_list = [ 8 , 5 , 9 , 11 , 3 , 7 ]
print ("The original list is : " + str (test_list))
K = 3
test_list.sort()
res = max (test_list[ 0 ] * test_list[ 1 ] * test_list[ - 1 ], test_list[ - 1 ] * test_list[ - 2 ] * test_list[ - 3 ])
print ("Maximum product using K elements : " + str (res))
|
Output :
The original list is : [8, 5, 9, 11, 3, 7]
Maximum product using K elements : 792
Time Complexity: O(nlogn) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), constant extra space is needed
Method #2 : Using max() + reduce() + combination() + mul + list comprehension The combinations of above functions can be used to solve this problem. In this, we extract each possible maximum using combinations and mul is used to perform multiplication. This can be applied with any possible number of K and is recommended method to solve this. Works only for Python 2.
Python
from itertools import combinations
from operator import mul
test_list = [ 8 , 5 , 9 , 11 , 3 , 7 ]
print ("The original list is : " + str (test_list))
K = 4
res = max ([ reduce (mul, ele) for ele in combinations(test_list, K)])
print ("Maximum product using K elements : " + str (res))
|
Output :
The original list is : [8, 5, 9, 11, 3, 7]
Maximum product using K elements : 5544
Method #4: Using Sorting + List Slicing + Product
Step-by-step approach:
Initialize the list test_list with some integer values.
Print the original list by using the print() function and converting the list to a string using the str() function.
Initialize the variable K to the number of elements to consider while finding the maximum product.
Sort the test_list in non-decreasing order using the sorted() function.
Find the product of the last K elements of the sorted list using the reduce() function from the functools module.
Print the result using the print() function and converting the maximum product to a string using the str() function.
Python3
from functools import reduce
test_list = [ 8 , 5 , 9 , 11 , 3 , 7 ]
print ( "The original list is : " + str (test_list))
K = 4
sorted_list = sorted (test_list)
res = reduce ( lambda x, y: x * y, sorted_list[ - K:])
print ( "Maximum product using K elements : " + str (res))
|
Output
The original list is : [8, 5, 9, 11, 3, 7]
Maximum product using K elements : 5544
Time Complexity: O(N log N).
Auxiliary Space: O(N).
Last Updated :
22 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...