Python | Maximum modulo pair
Sometimes, we need to find the specific problem of getting the pair which yields the maximum remainder. In some case, we don’t with to change the ordering of list and perform some operation in the 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 modulo, combination function finds all remainders internally and lambda function is used to compute the modulo.
Python3
# Python3 code to demonstrate # Maximum modulo pair # 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 modulo pair res = max (combinations(test_list, 2 ), key = lambda sub: sub[ 0 ] % sub[ 1 ]) # print result print ("The maximum remainder pair is : " + str (res)) |
The original list : [3, 4, 1, 7, 9, 1] The maximum remainder pair is : (7, 9)
Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), constant extra space required
Method #2 : Using list comprehension + nlargest() + combination() + lambda This method has potential of not only finding a single maximum but also k maximum modulo pairs if required and uses nlargest function instead of max function to achieve this functionality.
Python3
# Python3 code to demonstrate # Maximum modulo pair # using list comprehension + nlargest() + combinations() + lambda from itertools import combinations from heapq import nlargest # initializing list test_list = [ 3 , 4 , 1 , 7 , 9 , 8 ] # printing original list print ("The original list : " + str (test_list)) # using list comprehension + max() + combinations() + lambda # Maximum modulo pair # computes 2 maximum pair remainders res = nlargest( 2 , combinations(test_list, 2 ), key = lambda sub: sub[ 0 ] % sub[ 1 ]) # print result print ("The maximum remainder pair is : " + str (res)) |
The original list : [3, 4, 1, 7, 9, 8] The maximum remainder pair is : [(7, 9), (7, 8)]
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 Brute Force
Approach
this approach uses nested loops to generate all possible pairs of elements from the given list and calculate their modulo. It then returns the pair with the maximum modulo.
Algorithm
1. Define a function max_modulo_pair that takes a list as input.
2. Initialize two variables max_pair and max_mod to hold the maximum remainder pair and the maximum remainder value respectively.
3. Loop through each pair of elements in the list.
4. Compute the remainder of the sum of the two elements, and if it is greater than max_mod, update max_mod to this value and max_pair to the current pair.
5. Return max_pair.
Python3
def max_modulo_pair(lst): max_pair = None max_mod = - 1 for i in range ( len (lst)): for j in range (i + 1 , len (lst)): mod_val = (lst[i] + lst[j]) % max (lst[i], lst[j]) if mod_val > max_mod: max_mod = mod_val max_pair = (lst[i], lst[j]) return max_pair lst = [ 3 , 4 , 1 , 7 , 9 , 1 ] print (max_modulo_pair(lst)) |
(7, 9)
Time Complexity: O(n^2)
Space Complexity: O(1)