Sometimes, we need to find the specific problem of getting the pair which yields the maximum difference, 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 difference, combination function finds all differences internally and lambda function is used to compute the difference.
# Python3 code to demonstrate # maximum difference 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 difference pair res = max (combinations(test_list, 2 ), key = lambda sub: abs (sub[ 0 ] - sub[ 1 ]))
# print result print ("The maximum difference pair is : " + str (res))
|
The original list : [3, 4, 1, 7, 9, 1] The maximum difference pair is : (1, 9)
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using 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 difference pairs if required and uses nlargest function instead of max function to achieve this functionality.
# Python3 code to demonstrate # maximum difference 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 difference pair # computes 2 maximum pair differences res = nlargest( 2 , combinations(test_list, 2 ),
key = lambda sub: abs (sub[ 0 ] - sub[ 1 ]))
# print result print ("The maximum difference pair is : " + str (res))
|
The original list : [3, 4, 1, 7, 9, 8] The maximum difference pair is : [(1, 9), (1, 8)]
Method #3 : Using the built-in function reduce
This approach involves using the built-in function reduce to find the maximum and minimum elements in the list in a single pass. The maximum difference pair is then the pair of the maximum and minimum elements.
from functools import reduce
def findMaxDiffPair(lst):
# Print the original list
print ( "The original list:" , lst)
# Find the maximum and minimum elements in the list in a single pass using reduce
max_elem, min_elem = reduce ( lambda x, y: ( max (x[ 0 ], y), min (x[ 1 ], y)), lst, (lst[ 0 ], lst[ 0 ]))
# Return the maximum difference pair
max_diff_pair = (min_elem, max_elem)
print ( "The maximum difference pair:" , max_diff_pair)
return max_diff_pair
lst = [ 3 , 4 , 1 , 7 , 9 , 8 ]
findMaxDiffPair(lst) #This code is contributed by Edula Vinay Kumar Reddy |
The original list: [3, 4, 1, 7, 9, 8] The maximum difference pair: (1, 9)
Time complexity: O(n)
Auxiliary space: O(1)
Method #4: Using NumPy library
We can use the numpy.ptp function to find the maximum difference between any two elements in the given list.
Algorithm:
Import the NumPy library.
Initialize the list.
Print the original list.
Use the numpy.ptp function to find the maximum difference between any two elements in the list.
Print the maximum difference pair.
# Python3 code to demonstrate # maximum difference pair # using NumPy library import numpy as np
# initializing list test_list = [ 3 , 4 , 1 , 7 , 9 , 8 ]
# printing original list print ( "The original list : " + str (test_list))
# using NumPy library # maximum difference pair res = np.ptp(test_list)
# print result print ( "The maximum difference pair is : (" + str ( min (test_list)) + ", " + str ( max (test_list)) + ")" )
|
Output: The original list : [3, 4, 1, 7, 9, 8] The maximum difference pair is : (1, 9)
Time Complexity: O(n), where n is the length of the input list. The np.ptp function has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), as we’re not using any additional space other than the input list itself.
METHOD 5:Using nested loop.
APPROACH:
This program finds the maximum difference between two elements of a list and returns the pair of elements that have this difference.
ALGORITHM:
1. Initialize max_diff and max_pair variables to -1 and None respectively
2. Loop through all possible pairs of elements in the list
3. Calculate the difference between the elements in each pair and compare it with max_diff
4. If the difference is greater than max_diff, update max_diff and max_pair
5. Output max_pair as the maximum difference pair
# Input list lst = [ 3 , 4 , 1 , 7 , 9 , 1 ]
# Initializing variables to store the maximum difference and the pair with maximum difference max_diff = - 1
max_pair = None
# Looping through all possible pairs and finding the pair with maximum difference for i in range ( len (lst)):
for j in range (i + 1 , len (lst)):
if lst[j] - lst[i] > max_diff:
max_diff = lst[j] - lst[i]
max_pair = (lst[i], lst[j])
# Output the pair with maximum difference print ( "The maximum difference pair is:" , max_pair)
|
The maximum difference pair is: (1, 9)
Time complexity: O(n^2) where n is the length of the input list, due to nested loops through all possible pairs of elements.
Space complexity: O(1), as we are only storing a few variables for the maximum difference and pair.