Skip to content

Instantly share code, notes, and snippets.

@miry
Created February 27, 2013 12:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miry/5047429 to your computer and use it in GitHub Desktop.
Save miry/5047429 to your computer and use it in GitHub Desktop.
def pairs(numbers, diff)
counter = 0
numbers.sort!
n = numbers.length
numbers.each_with_index do |val, i|
j = i + 1
subject = val + diff
j += 1 while j < n && numbers[j] < subject
while numbers[j] == subject
counter += 1
j += 1
end
end
puts counter
end
n, k = gets.split.map!(&:to_i)
numbers = gets.split(" ").map!(&:to_i)
pairs(numbers, k)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment