Skip to content

Instantly share code, notes, and snippets.

@ericxtang
Created August 7, 2011 10:21
Show Gist options
  • Save ericxtang/1130273 to your computer and use it in GitHub Desktop.
Save ericxtang/1130273 to your computer and use it in GitHub Desktop.
k difference for interview street challenge
nk = STDIN.gets.split(" ")
nums_str = STDIN.gets.split(" ")
n = nk[0].to_i
k = nk[1].to_i
nums = nums_str.collect{|num| num.to_i}.sort!
h = {}
count = 0
nums.each do |n|
if h[(n-k).to_s]
count += 1
end
h[n.to_s] = n
end
puts count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment