Open In App

Python – Maximum product using K elements

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate
# Maximum product using K elements
# using max() + sort() + list comprehension
 
# Initializing list
test_list = [8, 5, 9, 11, 3, 7]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing K
K = 3
 
# Maximum product using K elements
# using max() + sort() + list comprehension
test_list.sort()
res = max(test_list[0] * test_list[1] * test_list[-1], test_list[-1] * test_list[-2] * test_list[-3])
 
# printing result
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




# Python code to demonstrate
# Maximum product using K elements
# using max() + reduce() + combination() + mul + list comprehension
from itertools import combinations
from operator import mul
 
# Initializing list
test_list = [8, 5, 9, 11, 3, 7]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing K
K = 4
 
# Maximum product using K elements
# using max() + reduce() + combination() + mul + list comprehension
res = max([reduce(mul, ele) for ele in combinations(test_list, K)])
 
# printing result
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
 
# Initializing list
test_list = [8, 5, 9, 11, 3, 7]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing K
K = 4
 
# Maximum product using K elements
# using sorting + list slicing + product
sorted_list = sorted(test_list)
res = reduce(lambda x, y: x * y, sorted_list[-K:])
 
# printing result
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
Previous
Next
Share your thoughts in the comments
Similar Reads