Python - Maximum Sum Record
Last Updated :
09 Apr, 2023
Sometimes, while working with data, we might have a problem in which we need to find maximum sum 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 comprehension The combination of this functions can be used to perform this task. In this, we compute the sum of all pairs and then return the max of it using max().
Python3
# Python3 code to demonstrate working of
# Maximum Sum Record
# Using list comprehension + max()
# initialize list
test_list = [(3, 5), (1, 7), (10, 3), (1, 2)]
# printing original list
print("The original list : " + str(test_list))
# Maximum Sum Record
# Using list comprehension + max()
temp = [b + a for a, b in test_list]
res = max(temp)
# printing result
print("Maximum sum among pairs : " + str(res))
Output : The original list : [(3, 5), (1, 7), (10, 3), (1, 2)]
Maximum sum among pairs : 13
Time complexity: O(n), where n is the length of the test_list.
Auxiliary space: O(n), where n is the length of the test_list.
Method #2 : Using max() + lambda This is similar to above method. In this the task performed by list comprehension is solved using lambda function, providing the sum computation logic. Returns the max. sum pair.
Python3
# Python3 code to demonstrate working of
# Maximum Sum Record
# Using lambda + max()
# initialize list
test_list = [(3, 5), (1, 7), (10, 3), (1, 2)]
# printing original list
print("The original list : " + str(test_list))
# Maximum Sum Record
# Using lambda + max()
res = max(test_list, key = lambda sub: sub[1] + sub[0])
# printing result
print("Maximum sum among pairs : " + str(res))
Output : The original list : [(3, 5), (1, 7), (10, 3), (1, 2)]
Maximum sum among pairs : 13
Time Complexity: O(n) where n is the number of elements in the string list. The max() + lambda is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list.
Method #3 : Using Heapq.nlargest
Python3
#Method #3 : Using Heapq.nlargest
#Importing heapq module for nlargest()
import heapq
#initialize list
test_list = [(3, 5), (1, 7), (10, 3), (1, 2)]
#printing original list
print("The original list : " + str(test_list))
#Maximum Sum Record
#Using heapq.nlargest
res = heapq.nlargest(1, test_list, key=lambda x: x[0] + x[1])[0]
#printing result
print("Maximum sum among pairs : " + str(sum(res)))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original list : [(3, 5), (1, 7), (10, 3), (1, 2)]
Maximum sum among pairs : 13
Time complexity: O(n log k), where k is the number of elements to retrieve.
Auxiliary Space: O(k), since we only store k elements in the heap.
Similar Reads
Python | Maximum Sum Sublist The task is to find a contiguous sublist (i.e., a sequence of elements that appear consecutively in the original list) such that the sum of the elements in this sublist is as large as possible. We need to return the maximum sum of this sublist. Let's explore methods to find Maximum Sum Sublist in py
2 min read
Python | Maximize Record list Sometimes, while working with Python records, we can have a problem in which we need to perform cross maximization of list of tuples. This kind of application is popular in web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using list comprehension +
6 min read
Python - Row with Maximum Record Element Sometimes, while working with Python Records, we can have a problem in which we need to find the row with maximum record element. This kind of problem can come in domains of web development and day-day programming. Let's discuss certain ways in which this task can be performed. Input : test_list = [
7 min read
Python - Maximum in Row Range Given a range and a Matrix, extract the maximum element out of that range of rows. Input : test_list = [[4, 3, 6], [9, 1, 3], [4, 5, 2], [9, 10, 3], [5, 9, 12], [3, 14, 2]], i, j = 2, 5 Output : 12 Explanation : Checks for rows 2, 3 and 4, maximum element is 12. Input : test_list = [[4, 3, 6], [9, 1
5 min read
Python - Records Maxima in List of Tuples Sometimes, while working with records, we can have a problem in which we need to the maximum all the columns of a container of lists that are tuples. This kind of application is common in the web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using ma
5 min read
Python - Maximum record value key in dictionary Sometimes, while working with dictionary records, we can have a problem in which we need to find the key with maximum value of a particular key of nested records in list. This can have applications in domains such as web development and Machine Learning. Lets discuss certain ways in which this task
6 min read