Print the count of pairs in an array which difference is equal to +4 or -4
Problem to find the pairs in an array which difference is equal to +4 or -4
Example-1:
Input : n = [ 10, 11, 12, 13, 14, 15, 18] Output : (10, 14) (11, 15) (14, 10) (14, 18) (15, 11) (18, 14)
Example-2:
Input : n = [ 1, 2, 3, 4, 5] Output : (1, 5) (5, 1)
Solution
n = [ 10, 11, 12, 13, 14, 15, 18] n.append([]) for i in range(1,len(n)): for j in range(1,len(n)): if(i == j): continue else: a = n[i-1]-n[j-1] if(a==4 or a==-4): print((n[i-1],n[j-1]),end = " ")
Output
(10, 14) (11, 15) (14, 10) (14, 18) (15, 11) (18, 14)