#include using namespace std; typedef long long int ll; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, d; cin >> n >> d; map pre, suf; vector a(n); for (int i = 0; i < n; i++) { cin >> a[i]; suf[a[i]]++; } ll res = 0; for (int i = 0; i < n; i++) { suf[a[i]]--; res += pre[a[i] - d] * suf[a[i] + d]; pre[a[i]]++; } cout << res << endl; }