Python - Find Minimum Pair Sum in list
Last Updated :
09 Apr, 2023
Sometimes, we need to find the specific problem of getting the pair which yields the minimum 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 + min() + 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 min function to get the minimum sum, combination function finds all sums internally and lambda function is used to compute the sum.
Python3
# Python3 code to demonstrate
# Minimum Pair Sum
# using list comprehension + min() + 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 + min() + combinations() + lambda
# Minimum Pair Sum
res = min(combinations(test_list, 2), key = lambda sub: abs(sub[0]-sub[1]))
# print result
print("The minimum sum pair is : " + str(res))
Output : The original list : [3, 4, 1, 7, 9, 1]
The minimum sum pair is : (1, 1)
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using list comprehension + min() + 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 + nsmallest() + combination() + lambda This method has potential of not only finding a single minimum but also k minimum sum pairs if required and uses nsmallest function instead of min function to achieve this functionality.
Python3
# Python3 code to demonstrate
# Minimum Pair Sum
# using list comprehension + nsmallest() + combinations() + lambda
from itertools import combinations
from heapq import nsmallest
# 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
# Minimum Pair Sum
# computes 2 min sum pair
res = nsmallest(2, combinations(test_list, 2), key = lambda sub: abs(sub[0] + sub[1]))
# print result
print("The minimum sum pair is : " + str(res))
Output : The original list : [3, 4, 1, 7, 9, 8]
The minimum sum pair is : [(3, 1), (4, 1)]
Time Complexity: O(n*n) where n is the number of elements in the string list. The list comprehension + nsmallest() + combination() + lambda is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res test_list.
Method #3 : Using for loop
We start by initializing the minimum pair sum to the maximum possible value, using float('inf'). We loop through all pairs of distinct elements in the list, using two nested loops. Note that we start the second loop at i+1, since we don't want to count pairs with the same element twice. For each pair, we calculate the sum of the two elements. If the current pair sum is less than the current minimum pair sum, we update the minimum pair sum and the minimum pair. After we've looped through all pairs, we print the minimum sum pair.
Python3
lst = [3, 4, 1, 7, 9, 1]
# Initialize the minimum pair sum to the maximum possible value
min_pair_sum = float('inf')
# Loop through all pairs of distinct elements in the list
for i in range(len(lst)):
for j in range(i+1, len(lst)):
# Calculate the sum of the current pair
pair_sum = lst[i] + lst[j]
# If the current pair sum is less than the current minimum pair sum,
# update the minimum pair sum and the minimum pair
if pair_sum < min_pair_sum:
min_pair_sum = pair_sum
min_pair = (lst[i], lst[j])
print("The minimum sum pair is :", min_pair)
OutputThe minimum sum pair is : (1, 1)
Time complexity: O(n^2)
Auxiliary Space: O(1)
Similar Reads
Python - Minimum Product Pair in List
Sometimes, we need to find the specific problem of getting the pair that yields the minimum product, this can be solved by sorting and getting the first and second elements of the list. But in some case, we donât with to change the ordering of list and perform some operation in the similar list with
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 - Column Minimum in Tuple list
Sometimes, while working with records, we can have a problem in which we need to find min of all the columns of a container of lists which are tuples. This kind of application is common in web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using min()
6 min read
Python - Closest Sum Pair in List
Sometimes, we desire to get the elements that sum to a particular element. But in cases we are not able to find that, our aim changes to be one to find the closest one. This can have application in many domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using diction
4 min read
Python | Minimum element in tuple list
Sometimes, while working with data in form of records, we can have a problem in which we need to find the minimum element of all the records received. This is a very common application that can occur in Data Science domain. Let's discuss certain ways in which this task can be performed. Method #1 :
5 min read
Python - Minimum in tuple list value
Many times, while dealing with containers in any language we come across lists of tuples in different forms, tuples in themselves can have sometimes more than native datatypes and can have list as their attributes. This article talks about the minimum of list as tuple attribute. Letâs discuss certai
5 min read
Python program to find smallest number in a list
In this article, we will discuss various methods to find smallest number in a list. The simplest way to find the smallest number in a list is by using Python's built-in min() function.Using min()The min() function takes an iterable (like a list, typle etc.) and returns the smallest value.Pythona = [
2 min read
Python - Find minimum k records from tuple list
Sometimes, while working with data, we can have a problem in which we have records and we require to find the lowest K scores from it. This kind of application is popular in web development domain. Letâs discuss certain ways in which this problem can be solved. Method #1 : Using sorted() + lambda Th
6 min read
Python | Consecutive Pair Minimums
Sometimes, while working with Python list, one can have a problem in which one needs to find perform the minimum 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 solved.
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