I am trying to find the number of pairs in a list of numbers with a specific difference. Say, with the list
1 2 3 4 5
and the difference target '2', I would want to print the number '3' because there are 3 pairs in this sequence with a difference of '2'. however, my code is super slow - it double-counts all of the pairs, and so I end up needing to divide my solutions by 2 to get the answer. Is there a way to accomplish this same task without double-counting? I appreciate any insights you might have. thanks! code is printed below
import sys
def main():
solutions=0
pairs=[]
for i in xrange(len(numbers)):
for j in xrange(len(numbers)):
if i!=j:
pairs.append([numbers[i], numbers[j]])
for pair in pairs:
if abs(pair[0]-pair[1])==k:
solutions+=1
else:
continue
return solutions/2
if __name__ == '__main__':
lines=sys.stdin.readlines()
n,k=map(int, lines[0].strip().split())
numbers=map(int, lines[1].strip().split())
print main()