Python | Find all distinct pairs with difference equal to k
Given a list of integers and a positive integer k, write a Python program to count all distinct pairs with difference equal to k.
Examples:
Input : [1, 5, 3, 4, 2], k = 3 Output : [(5, 2), (4, 1)] Input : [8, 12, 16, 4, 0, 20], k = 4 Output : [(8, 4), (12, 8), (16, 12), (4, 0), (20, 16)]
Approach #1 : Python list comprehension
We will use list comprehension using two loops using ‘e1’ and ‘e2’ that will traverse given list. We check if e1-e2 == k or not and return the (e1, e2) tuple respectively. Finally, a list with required tuples will be returned.
# Python3 program to Find all distinct # pairs with difference equal to k def findPairs(lst, k): return [(e1, e2) for e1 in lst for e2 in lst if (e1-e2 == k)] # Driver code lst = [1, 5, 3, 4, 2] k = 3print(findPairs(lst, k)) |
[(5, 2), (4, 1)]
Approach #2 :
This is an efficient approach with respect to the above approach as it uses only O(n) space. We take an empty list to store the output. Then, we use a loop with iterator ‘e’ to traverse through given list. In every iteration, we check is e+k i.e the required pair integer for e is available or not. If yes, we append the tuple to ‘res’.
# Python3 program to Find all distinct # pairs with difference equal to k def findPairs(lst, k): res = [] for e in lst: if e + k in lst: res.append(tuple((e, e + k))) return res # Driver code lst = [1, 5, 3, 4, 2] k = 3print(findPairs(lst, k)) |
[(1, 4), (2, 5)]
Approach #3 : combinations() from itertools module
The best approach is to use the inbuilt function from itertools module. combinations() produces an iterator over tuples of all combinations of n elements in inputs. We make the use of these combinations and ouptut those having ‘k’ difference.
# Python3 program to Find all distinct # pairs with difference equal to k from itertools import combinations def findPairs(lst, k): return [(x, y) for x, y in combinations(lst, r = 2) if abs(x - y) == k] # Driver code lst = [1, 5, 3, 4, 2] k = 3print(findPairs(lst, k)) |
[(1, 4), (5, 2)]
Recommended Posts:
- Python program to find sum of absolute difference between all pairs in a list
- Python program to find all possible pairs with given sum
- Python | Find Maximum difference pair
- Python Set difference to find lost element from a duplicated array
- Python set operations (union, intersection, difference and symmetric difference)
- Count distinct elements in an array in Python
- Print first n distinct permutations of string using itertools in Python
- Python | Minimum number of subsets with distinct elements using Counter
- Program to print all distinct elements of a given integer array in Python | Ordered Dictionary
- Python | Get first N key:value pairs in given dictionary
- Given two arrays, find n+m-1 unique sum pairs
- Python Set | Pairs of complete strings in two sets
- Python | Make pair from two list such that elements are not same in pairs
- numpy.equal() in Python
- Python | Split given string into equal halves
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.



