Open In App

Python – Maximum Quotient Pair in List

Last Updated : 09 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 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




# Python3 code to demonstrate
# Maximum Quotient Pair in List
# 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
# Maximum Quotient Pair in List
res = max(combinations(test_list, 2), key = lambda sub: sub[0] // sub[1])
 
# print result
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




# Python3 code to demonstrate
# Maximum Quotient Pair in List
# using list comprehension + nlargest() + combinations() + lambda
from itertools import combinations
from heapq import nlargest
 
# 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
# Maximum Quotient Pair in List
# computes 2 maximum pair differences
res = nlargest(2, combinations(test_list, 2), key = lambda sub: sub[0] // sub[1])
 
# print result
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




#Python3 code to demonstrate
#Maximum Quotient Pair in List
#using sorted() + loop + lambda
#initializing list
test_list = [3, 4, 1, 7, 9, 1]
 
#printing original list
print("The original list : " + str(test_list))
 
#sorting the list in decreasing order
test_list = sorted(test_list, reverse = True)
 
#using sorted() + loop + lambda
#Maximum Quotient Pair in List
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 result
print("The maximum quotient pair is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


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




# Import the required libraries
# Initialize the test_list
test_list = [3, 4, 1, 7, 9, 1]
# Print the original list
print("The original list : " + str(test_list))
# Initialize the maximum quotient as the negative infinity and 1
max_quotient = (float('-inf'), 1)
# Loop through the elements in the test_list
for i in range(len(test_list) - 1):
    for j in range(i + 1, len(test_list)):
        # Check if the division doesn't result in a ZeroDivisionError
        try:
            # If the quotient of the current pair is greater than the maximum, update the maximum quotient
            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])
        # If a ZeroDivisionError occurs, pass
        except ZeroDivisionError:
            pass
# Store the result in the res variable
res = max_quotient
# Print the result
print("The maximum quotient pair is : " + str(res))
 
 
#This code is contributed by Jyothi pinjala.


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) 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads