def beau(vals, diff):
    lookup = set(vals)
    result = []

    for x in vals:
        if x - d in lookup and x + d in lookup:
            result.append((x - d, x, x + d))

    return result

n, d     = map(int, input().split())
values   = tuple(map(int, input().split()))
triplets = beau(values, d)

print(len(triplets))

def beau(vals, diff):
    lookup = set(vals)
    result = []

    for x in vals:
        if x - diff in lookup and x + diff in lookup:
            result.append((x - diff, x, x + diff))

    return result

n, d     = map(int, input().split())
values   = tuple(map(int, input().split()))
triplets = beau(values, d)

print(len(triplets))

Update solution.py