# frozen_string_literal: true in_n, in_d = gets.chomp.split.map(&:to_i) in_a = gets.chomp.split.map(&:to_i) tally = Hash.new { |h, k| h[k] = 0} res = 0 in_a.each do |a| tally[a] += 1 res += tally[a-in_d] * tally[a-in_d*2] end puts res