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.
Similar Reads
Python | Find Maximum difference between tuple pairs
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 compr
5 min read
Python | Maximum Difference in String
Sometimes, we might have a problem in which we require to get the maximum difference of 2 numbers from Strings but with a constraint of having the numbers in successions. This type of problem can occur while competitive programming. Letâs discuss certain ways in which this problem can be solved. Met
4 min read
Python - Get minimum difference in Tuple pair
Sometimes, while working with data, we might have a problem in which we need to find minimum 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 min() + list compr
4 min read
Python - Maximum difference across lists
Given two lists, the task is to write a Python program to find maximum difference among like index elements. Examples: Input : test_list1 = [3, 4, 2, 1, 7], test_list2 = [6, 2, 1, 9, 1]Output : 8Explanation : 9 - 1 = 8 is maximum difference across lists in same index. Input : test_list1 = [3, 4, 2,
4 min read
Python | Minimum Difference in Matrix Columns
This particular article focuses on a problem that has utility in competitive as well as day-day programming. Sometimes, we need to get the minimum difference between the like indices when compared with the next list. The minimum difference between the like elements in that index is returned. Letâs d
3 min read
Python - Maximum Aggregation Pair in List
Sometimes, we need to find the specific problem of getting the pair which yields the maximum sum, this can be computed by getting initial two elements after sorting. But in some case, we donât with to change the ordering of list and perform some operation in the similar list without using extra spac
3 min read
Python | Record Point with Minimum difference
Sometimes, while working with data, we might have a problem in which we need to find minimum 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 min() + list compr
2 min read
Python | Maximum absolute difference list of list
This particular article focuses on a problem that has utility in competitive as well as day-day programming. Sometimes, we need to get the maximum difference between the like indices when compared with the next list. The maximum difference between the like elements in that index is returned. Let's d
7 min read
Python - Maximum Quotient Pair in List
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 withou
5 min read
Python - Dual Element row with Maximum difference
Sometimes, while working with Python Matrix, we can have Matrix with its elements to be rows with just two elements and we may desire to get row with elements having maximum difference. This can have application in many domains. Lets discuss certain ways in which this task can be performed. Method #
6 min read