Python - Maximum Aggregation Pair in List
Last Updated :
20 Mar, 2023
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 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 sum, combination function finds all sums internally and lambda function is used to compute the sum.
Python3
# Python3 code to demonstrate
# Maximum Aggretation Pair in List
# 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 Aggretation Pair in List
res = max(combinations(test_list, 2), key = lambda sub: sub[0] + sub[1])
# print result
print("The maximum sum pair is : " + str(res))
Output : The original list : [3, 4, 1, 7, 9, 1]
The maximum sum 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 is required
Method #2 : Using list comprehension + nlargest() + combination() + lambda This method has potential of not only finding a single maximum but also k maximum sum pairs if required and uses nlargest function instead of max function to achieve this functionality.
Python3
# Python3 code to demonstrate
# Maximum Aggretation Pair in List
# 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 + nsmallest() + combinations() + lambda
# Maximum Aggretation Pair in List
# computes 2 max sum pair
res = nlargest(2, combinations(test_list, 2), key = lambda sub: sub[0] + sub[1])
# print result
print("The maximum sum pair is : " + str(res))
Output : The original list : [3, 4, 1, 7, 9, 8]
The maximum sum pair is : [(9, 8), (7, 9)]
Similar Reads
Python - Maximum of Consecutive Pair in integer list Sometimes, while working with Python list, one can have a problem in which one needs to find perform the maximization of list in pair form. This is useful as a subproblem solution of bigger problem in web development and day-day programming. Letâs discuss certain ways in which this problem can be so
5 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 | Find Maximum difference pair 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 with
5 min read
Python | Positions of maximum element in list Sometimes, while working with Python lists, we can have a problem in which we intend to find the position of maximum element of list. This task is easy and discussed many times. But sometimes, we can have multiple maximum elements and hence multiple maximum positions. Let's discuss a shorthand to ac
3 min read
Python - Maximum of K element in other list Given two lists, extract maximum of elements with similar K in corresponding list. Input : test_list1 = [4, 3, 6, 2, 8], test_list2 = [3, 6, 3, 4, 3], K = 3 Output : 8 Explanation : Elements corresponding to 3 are, 4, 6, and 8, Max. is 8. Input : test_list1 = [10, 3, 6, 2, 8], test_list2 = [5, 6, 5,
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