Python – Maximum Quotient Pair in List
Last Updated :
09 Apr, 2023
Sometimes, we need to find the specific problem of getting the pair which yields the maximum Quotient, this can be solved by sorting and getting the first and last elements of the list. But in some case, we don’t with to change the ordering of list and perform some operation in a 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 quotient, combination function finds all quotients internally and lambda function is used to compute the quotients.
Python3
from itertools import combinations
test_list = [ 3 , 4 , 1 , 7 , 9 , 1 ]
print ( "The original list : " + str (test_list))
res = max (combinations(test_list, 2 ), key = lambda sub: sub[ 0 ] / / sub[ 1 ])
print ( "The maximum quotient pair is : " + str (res))
|
Output
The original list : [3, 4, 1, 7, 9, 1]
The maximum quotient pair is : (9, 1)
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using the list comprehension + max() + combination() + lambda which has a time complexity of O(n*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 #2 : Using list comprehension + nlargest() + combination() + lambda This method has potential of not only finding a single maximum but also k maximum quotient pairs if required and uses nlargest function instead of max function to achieve this functionality.
Python3
from itertools import combinations
from heapq import nlargest
test_list = [ 3 , 4 , 1 , 7 , 9 , 1 ]
print ( "The original list : " + str (test_list))
res = nlargest( 2 , combinations(test_list, 2 ), key = lambda sub: sub[ 0 ] / / sub[ 1 ])
print ( "The maximum quotient pair is : " + str (res))
|
Output
The original list : [3, 4, 1, 7, 9, 1]
The maximum quotient pair is : [(9, 1), (7, 1)]
Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method #3 : Using sorted() + loop + lambda
This method sorts the list in decreasing order and then traverse the list to find the maximum quotient pair
Python3
test_list = [ 3 , 4 , 1 , 7 , 9 , 1 ]
print ( "The original list : " + str (test_list))
test_list = sorted (test_list, reverse = True )
res = max ([(a,b) for i, a in enumerate (test_list) for j, b in enumerate (test_list) if i < j], key = lambda x: x[ 0 ] / x[ 1 ])
print ( "The maximum quotient pair is : " + str (res))
|
Output
The original list : [3, 4, 1, 7, 9, 1]
The maximum quotient pair is : (9, 1)
Time Complexity: O(n^2)
Auxiliary Space: O(1) as only extra variables are used in loop.
Method #4 : Using a for loop:
Python3
test_list = [ 3 , 4 , 1 , 7 , 9 , 1 ]
print ( "The original list : " + str (test_list))
max_quotient = ( float ( '-inf' ), 1 )
for i in range ( len (test_list) - 1 ):
for j in range (i + 1 , len (test_list)):
try :
if test_list[j] ! = 0 and test_list[i] / test_list[j] > max_quotient[ 0 ] / max_quotient[ 1 ]:
max_quotient = (test_list[i], test_list[j])
except ZeroDivisionError:
pass
res = max_quotient
print ( "The maximum quotient pair is : " + str (res))
|
Output
The original list : [3, 4, 1, 7, 9, 1]
The maximum quotient pair is : (9, 1)
Time Complexity: O(n^2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...