
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Minimum Pair Sum in List using Python
The Minimum pair sum is defined by finding the smallest possible sum of two numbers taken from a given list of numbers. It can be used to solve challenges when minimizing the total of two elements is important, such as reducing the cost, distance, or time necessary for a certain operation. In Python, we have some built?in functions like float(), sort(), combination(), and, range() will be used to find the minimum pair sum in a list.
Syntax
The following syntax is used in the examples-
float('inf')
The float() is a built?in method in Python that accepts the parameter to set the large infinity value.
sort()
The sort() is a built?in method in Python that default sorts the number in ascending order.
combinations()
This is a built?in method in Python that follows the itertools module of Python which collects the order of the element list but order doesn't matter. It helps to set the possibilities of the pair.
len()
This len() is a built?in method in Python that returns the object length.
Using Nested for Loop
The program uses the recursive function where loops iterate through lists to find the minimum pair along sum().
Example
In the following example, the program starts with function named min_pair that accepts the parameter nums to receive the value of input list. In this function, two variables min_sum and min_pair are initialized. min_sum is initialized to positive infinity using float('inf') and min_pair is initialized to an empty tuple. Next, two nested loop iterate over all pairs integer in the list. Then use the if?statement, where the pair sum is smaller than min_sum, min_sum is adjusted according to the new pair sum, and min_pair is modified to represent the current pair of numbers. After checking all pairings, the function returns both the minimum sum and the minimum pair. Moving ahead to create the input list integers in the variable num. Then use the function call in two variables? r_sum and res_pair. Finally, we are using the same variables as a parameter of print function to get the result.
def min_pair(nums): min_sum = float('inf') min_pair = () for i in range(len(nums)): for j in range(i + 1, len(nums)): pair_sum = nums[i] + nums[j] if pair_sum < min_sum: min_sum = pair_sum min_pair = (nums[i], nums[j]) return min_sum, min_pair # create the list num = [40, 21, 91, 50, 11] r_sum, res_pair = min_pair(num) print("Minimum pair sum of the list:", r_sum) print("Pair is:", res_pair)
Output
Minimum pair sum of the list: 32 Pair is: (21, 11)
Using Sorting
The program uses the sort() function and applies the sum to the lists index element, which will help find the minimum pair sum.
Example
In the following example, start the program with min_pair which takes a list of numbers as a parameter. In function, the built?in function sort is used to sort the list of numbers. The first two elements of the sorted list are added up to determine the minimum sum. The first two elements of the sorted list are converted into a tuple to provide the minimum pair. Both the minimum pair and minimum sum are returned by the function. Next, create the list and store it in the variable num. Then call the function named min_pair in the variables? r_sum and r_pair. Moving ahead to using the same variables in the print function to get the result.
def min_pair(nums): nums.sort() min_sum = nums[0] + nums[1] min_pair = (nums[0], nums[1]) return min_sum, min_pair # create the list num = [11, 10, 9, 5, 1] r_sum, res_pair = min_pair(num) print("Minimum pair sum from the list:", r_sum) print("Pair is:", res_pair)
Output
Minimum pair sum from the list: 6 Pair is: (1, 5)
Using sum() and tuple()
The program uses the sum() to add the minimum pair from the list and using the built?in method tuple it will set the result of the minimum pair in the form of tuple.
Example
In the following example, begin the program starts with a function named min_pair_sum that accepts the parameter nums to receive the list values from the variable num(through function call). In this function, the list is sorted in ascending order. By adding the first two elements to the sorted list, the minimum sum will be calculated. The sorted list of first two elements has been converted into a tuple to provide the minimum pair. Then the function returns the minimum pair as well as the minimum sum. Next, store the input list in the variable num. Then accepts the variable num in the function call named min_pair_sum and stores it in the variables? r_sum and res_pair. Finally, we are printing the result of minimum pair and minimum sum.
def min_pair_sum(nums): sorted_nums = sorted(nums) min_sum = sum(sorted_nums[:2]) min_pair = tuple(sorted_nums[:2]) return min_sum, min_pair # Create the List num = [10, 11, 12, 13, 14] r_sum, res_pair = min_pair_sum(num) print("Minimum pair sum:", r_sum) print("Pair:", res_pair)
Output
Minimum pair sum: 21 Pair: (10, 11)
Using itertools.combination
This program uses the combinations() function from the itertools module to generate all possible pairs of numbers from a given list. It then finds the pair with the lowest sum and returns both the sum along its pair.
Example
In the following example, using the itertools module combinations function, the find_min_pair_sum function generates all possible pairings of numbers from the given list. It then iterates through all pairings to find the pair with the minimum sum and returns both the minimum sum and the minimum pair. Then calling function pass the numbers in the form of list and outputs the result.
from itertools import combinations def find_min_pair_sum(nums): pairs = combinations(nums, 2) min_sum = float('inf') min_pair = () for pair in pairs: pair_sum = sum(pair) if pair_sum < min_sum: min_sum = pair_sum min_pair = pair return min_sum, min_pair # Create the list numbers = [4, 2, 9, 5, 1] result_sum, result_pair = find_min_pair_sum(numbers) print("Minimum pair sum:", result_sum) print("Pair:", result_pair)
Output
Minimum pair sum: 3 Pair: (2, 1)
Conclusion
We discussed the various method to solve the problem statement. Finding the smallest pair sum in a list is essential for various applications, such as cost optimization, distance calculations, and time efficiency analysis in problem-solving and data processing jobs.