Skip to content

Instantly share code, notes, and snippets.

@abadfish
Created February 8, 2017 18:20
Show Gist options
  • Save abadfish/50fd5af3fd597ac6eeb42521da9721a1 to your computer and use it in GitHub Desktop.
Save abadfish/50fd5af3fd597ac6eeb42521da9721a1 to your computer and use it in GitHub Desktop.
Find pairs in array with difference of n
number_list = [4, 3, 33, 237, 2, 95, 17, 6, 67, 9, 19, 54, 8, 18, 25, 31, 1, 101, 234, 236, 100, 63, 68, 69]
input = 2
def find_pairs(array)
pairs = []
array.each_with_index do |px1, i|
for px2 in array[i+1..-1]
potential_difference1 = px2 - px1
potential_difference2 = px1 - px2
input = 2
if input == potential_difference1 || input == potential_difference2
pairs.push([px1, px2])
end
end
end
return pairs.count
end
puts find_pairs(number_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment