Sometimes, while working with data, we might have a problem in which we need to find maximum difference between available pairs in list. This can be application to many problems in mathematics domain. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using max() + list comprehension
The combination of this functions can be used to perform this task. In this, we compute the absolute difference of all pairs and then return the max of it using max().
Python3
# Python3 code to demonstrate working of # Maximum difference tuple pair # Using list comprehension + max() # initialize list test_list = [( 3 , 5 ), ( 1 , 7 ), ( 10 , 3 ), ( 1 , 2 )] # printing original list print ( "The original list : " + str (test_list)) # Maximum difference tuple pair # Using list comprehension + max() temp = [ abs (b - a) for a, b in test_list] res = max (temp) # printing result print ( "Maximum difference among pairs : " + str (res)) |
The original list : [(3, 5), (1, 7), (10, 3), (1, 2)] Maximum difference among pairs : 7
Method #2 : Using max() + lambda
This is similar to above method. In this the task performed by list comprehension is solved using lambda function, providing the difference computation logic. Returns the max. difference pair.
Python3
# Python3 code to demonstrate working of # Maximum difference tuple pair # Using lambda + max() # initialize list test_list = [( 3 , 5 ), ( 1 , 7 ), ( 10 , 3 ), ( 1 , 2 )] # printing original list print ( "The original list : " + str (test_list)) # Maximum difference tuple pair # Using lambda + max() res = max (test_list, key = lambda sub: abs (sub[ 1 ] - sub[ 0 ])) # printing result print ( "Maximum difference among pairs : " + str (res)) |
The original list : [(3, 5), (1, 7), (10, 3), (1, 2)] Maximum difference among pairs : (10, 3)
Method #3: Using reduce() function and a lambda function
This approach uses the reduce() function to iterate through the elements of the list and a lambda function to compute the maximum difference between the elements of each tuple.
Python3
from functools import reduce # initialize list test_list = [( 3 , 5 ), ( 1 , 7 ), ( 10 , 3 ), ( 1 , 2 )] # printing original list print ( "The original list : " + str (test_list)) # Maximum difference tuple pair # Using reduce() function and lambda function max_pair = reduce ( lambda x, y: x if abs (x[ 1 ] - x[ 0 ]) > abs (y[ 1 ] - y[ 0 ]) else y, test_list) # printing result print ( "Maximum difference among pairs : " + str (max_pair)) #This code is contributed by Edula Vinay Kumar Reddy |
The original list : [(3, 5), (1, 7), (10, 3), (1, 2)] Maximum difference among pairs : (10, 3)
The time complexity of this method is also O(n) and Auxiliary space is O(1) as well.
Method 4 : using a for loop:
This code iterates over each pair in the list and compares the absolute difference of each pair to the maximum difference seen so far. If the absolute difference of the current pair is greater than the maximum difference seen so far, then the current pair becomes the new max_pair.
step by step approach of the code you provided:
- Initialize a list of tuples test_list with four tuples containing two elements each.
- Print the original list using the print() function and str() to convert the list to a string.
- Initialize two variables max_pair and max_diff. max_pair will eventually hold the tuple pair with the maximum absolute difference, and max_diff will hold the value of the maximum absolute difference seen so far.
- Loop through each tuple pair in the list using a for loop.
- Calculate the absolute difference between the two elements in the current pair using the abs() function.
- Check if the current difference is greater than the current maximum difference (max_diff). If it is, update the values of max_pair and max_diff.
- After the loop finishes, print the tuple pair with the maximum absolute difference using the print() function and str() to convert the tuple to a string.
Python3
# initialize list test_list = [( 3 , 5 ), ( 1 , 7 ), ( 10 , 3 ), ( 1 , 2 )] # printing original list print ( "The original list : " + str (test_list)) # Maximum difference tuple pair max_pair = None max_diff = float ( '-inf' ) for pair in test_list: diff = abs (pair[ 1 ] - pair[ 0 ]) if diff > max_diff: max_pair = pair max_diff = diff # printing result print ( "Maximum difference among pairs : " + str (max_pair)) |
The original list : [(3, 5), (1, 7), (10, 3), (1, 2)] Maximum difference among pairs : (10, 3)
The time complexity of this approach is O(n) since it iterates over the list once.
The space complexity is O(1) since it only stores a constant number of variables (max_pair and max_diff).
Method #5: Using map() and abs() with key argument
- First, we use map() and a lambda function to create a list of absolute differences between the pairs in test_list.
- Next, we use the index() method to find the index of the maximum absolute difference in the abs_diffs list.
- Finally, we use the max_index to find the corresponding tuple pair with the maximum difference in test_list.
Python3
test_list = [( 3 , 5 ), ( 1 , 7 ), ( 10 , 3 ), ( 1 , 2 )] # use map() with abs() and key argument to create list of absolute differences abs_diffs = list ( map ( lambda x: abs (x[ 0 ] - x[ 1 ]), test_list)) # use index() to find index of maximum absolute difference max_index = abs_diffs.index( max (abs_diffs)) # use index to find corresponding tuple pair with max difference max_pair = test_list[max_index] print ( "Maximum difference among pairs : " + str (max_pair)) |
Maximum difference among pairs : (10, 3)
Time complexity: O(n), where n is the length of test_list
Auxiliary space: O(n), where n is the length of test_list