Python program to find all possible pairs with given sum
Given a list of integers and an integer variable K, write a Python program to find all pairs in the list with given sum K.
Examples:
Input : lst =[1, 5, 3, 7, 9]
K = 12
Output : [(5, 7), (3, 9)]
Input : lst = [2, 1, 5, 7, -1, 4]
K = 6
Output : [(2, 4), (1, 5), (7, -1)]
Method #1 : Pythonic Naive
This is a naive approach to the above problem. First, we take an empty list ‘res’ and start a loop and traverse each element of the given list of integers. In each iteration, pop the element, store it in ‘num’, find remaining difference for sum K, and check if the difference exists in the given list or not.
# Python3 program to find all pairs in # a list of integers with given sum def findPairs(lst, K): res = [] while lst: num = lst.pop() diff = K - num if diff in lst: res.append((diff, num)) res.reverse() return res # Driver code lst = [1, 5, 3, 7, 9] K = 12print(findPairs(lst, K)) |
[(5, 7), (3, 9)]
Method #2 : Using collections.Counter
This approach follows the same method as discussed above using collections.Counter.
# Python3 program to find all pairs in # a list of integers with given sum from collections import Counter def findPairs(lst, K): res = [] count = Counter(lst) for x in lst: y = K - x if (x != y and count[y]) or (x == y and count[y] > 1): res.append((x, y)) count.subtract((x, y)) return res # Driver code lst = [1, 5, 3, 7, 9] K = 12print(findPairs(lst, K)) |
[(5, 7), (3, 9)]
Method #3 : itertools.combinations (Naive method)
This is a naive approach to use itertools.combinations. We use a for loop to traverse through each combination and find out the desired one.
# Python3 program to find all pairs in # a list of integers with given sum from itertools import combinations def findPairs(lst, K): res = [] for var in combinations(lst, 2): if var[0] + var[1] == K: res.append((var[0], var[1])) return res # Driver code lst = [1, 5, 3, 7, 9] K = 12print(findPairs(lst, K)) |
[(5, 7), (3, 9)]
Method #4 : itertools.combinations (Efficient method)
# Python3 program to find all pairs in # a list of integers with given sum from itertools import combinations def findPairs(lst, K): return [pair for pair in combinations(lst, 2) if sum(pair) == K] # Driver code lst = [1, 5, 3, 7, 9] K = 12print(findPairs(lst, K)) |
[(5, 7), (3, 9)]
Recommended Posts:
- Python program to find sum of absolute difference between all pairs in a list
- Python | Find Symmetric Pairs in dictionary
- Python | Find all distinct pairs with difference equal to k
- Python Program for Program to find area of a circle
- Python Program for Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Python Program to find sum of array
- Python Program to find whether a no is power of two
- Python program to find the first day of given year
- Python program to find IP Address
- Python program to find Cumulative sum of a list
- Python program to find day of the week for a given date
- Python Program for Find sum of Series with n-th term as n^2 - (n-1)^2
- Python Program for Find sum of odd factors of a number
- Python Program for Find sum of even factors of a number
- Python Program for Find the perimeter of a cylinder
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



