...

/

Solution: Find the Maximum Product of Two Integers in a List

Solution: Find the Maximum Product of Two Integers in a List

In this review lesson, we give a detailed analysis of the solution to find the maximum product of two integers in a list.

Solution #1: brute force approach

# Decimal library to assign infinite numbers
from decimal import Decimal
def find_max_prod(lst):
"""
Finds the pair having maximum product in a given list
:param lst: A list of integers
:return: A pair of integer
"""
max_product = Decimal('-Infinity')
max_i = -1
max_j = -1
for i in lst:
for j in lst:
if max_product < i * j and i is not j:
max_product = i * j
max_i = i
max_j = j
return max_i, max_j
# Driver to test above code
if __name__ == '__main__':
lst = [1, 3, 5, 2, 6]
num1, num2 = find_max_prod(lst)
print (num1, num2)

Explanation

In this solution, we are calculating the product of each element of the list with every other element, except for the element itself. Every time we calculate the product, we compare it with the previous stored product in max_product. If it is greater than max_product, we update the value of max_product and store the indexes of the list in i and j.

Time complexity

...