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.
The original list : [3, 4, 1, 7, 9, 1] The maximum sum pair is : (7, 9)
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.
The original list : [3, 4, 1, 7, 9, 8] The maximum sum pair is : [(9, 8), (7, 9)]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Aggregation in MongoDB using Python
- Python MongoDB - $group (aggregation)
- Write custom aggregation function in Pandas
- Python | Pair and combine nested list to tuple list
- Python - Maximum of Consecutive Pair in integer list
- Python - Maximum Quotient Pair in List
- Python List Comprehension to find pair with given sum from two arrays
- Python | Pair iteration in list
- Python | Make pair from two list such that elements are not same in pairs
- Python | Ways to join pair of elements in list
- Python | Pair summation of list elements
- Python | Pair the consecutive character strings in a list
- Python - Find Minimum Pair Sum in list
- Python - Minimum Product Pair in List
- Python - Count similar pair in Dual List
- Python - Numeric Sort in Mixed Pair String List
- Python - Closest Sum Pair in List
- Python - Convert each list element to key-value pair
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.