Python | Find Maximum difference pair
Last Updated :
18 May, 2023
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
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: abs (sub[ 0 ] - sub[ 1 ]))
print ("The maximum difference pair is : " + str (res))
|
Output :
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
from itertools import combinations
from heapq import nlargest
test_list = [ 3 , 4 , 1 , 7 , 9 , 8 ]
print ("The original list : " + str (test_list))
res = nlargest( 2 , combinations(test_list, 2 ),
key = lambda sub: abs (sub[ 0 ] - sub[ 1 ]))
print ("The maximum difference pair is : " + str (res))
|
Output :
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.
Python3
from functools import reduce
def findMaxDiffPair(lst):
print ( "The original list:" , lst)
max_elem, min_elem = reduce ( lambda x, y: ( max (x[ 0 ], y), min (x[ 1 ], y)), lst, (lst[ 0 ], lst[ 0 ]))
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)
|
Output
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
import numpy as np
test_list = [ 3 , 4 , 1 , 7 , 9 , 8 ]
print ( "The original list : " + str (test_list))
res = np.ptp(test_list)
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
Python3
lst = [ 3 , 4 , 1 , 7 , 9 , 1 ]
max_diff = - 1
max_pair = None
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])
print ( "The maximum difference pair is:" , max_pair)
|
Output
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.
Share your thoughts in the comments
Please Login to comment...