# frozen_string_literal: true # This file is expanded by nanacl. main = -> do # ================================================================= # 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 } has_one_tally = Hash.new { |h, k| h[k] = 0 } res = 0 in_a.each do |a| tally[a] += 1 has_one_tally[a] += tally[a - in_d] res += has_one_tally[a - in_d] end puts res end # -------------------------------------------------------------------------- # ============================================================================== main.call