• + 9 comments

    Simple. Iterate through the array once, and add each number to a dictionary. Then, iterate through a second time, and, for each number, see if that number + k exists in the library. If it does, then you know you have a pair, and can increment anwser

    def pairs(a, k)
        d = {}
        answer = 0
        for i in a:
            d[i] = i
        for j in a:
            g = j+k
            if g in d:
                answer +=1
        return answer