Perfect unique pair finding in sorted array
Problem to find the unique pair which has sum equal to k.
Example-1:
Input : n = [ 1, 2, 3, 4, 5] k = 7 Output : (2, 5) (3, 4)
Example-2:
Input : n = [ 5, 7, 12, 8, 9] k = 17 Output : (5, 12) (8, 9)
Solution
from itertools import combinations n = [ 1, 2, 3, 4, 5] k = 7 l = [] a = list(combinations(n,2)) for x in a: if(sum(x)==k): l.append(x) print(l)
Output
[(2, 5), (3, 4)]