HackerRank World Codesprint April: Beautiful Triplets

,

https://www.hackerrank.com/contests/world-codesprint-april/challenges/beautiful-triplets

problem

狭義単調増加な数列$a$および正整数$d$が与えられる。 $| \{ (i, j, k) \mid a_j - a_i = a_k - a_j = d \} |$を答えよ。

solution

Count. You can use a set. $O(N)$.

implementation

#!/usr/bin/env python3
n, d = map(int,input().split())
a = list(map(int,input().split()))
s = set(a)
ans = 0
for i in a:
    if i + d in s and i + 2*d in s:
        ans += 1
print(ans)