Skip to content

Instantly share code, notes, and snippets.

@willwright82
Created March 15, 2017 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save willwright82/69be1420cab7a822862a4d1600c96098 to your computer and use it in GitHub Desktop.
Save willwright82/69be1420cab7a822862a4d1600c96098 to your computer and use it in GitHub Desktop.
A Ruby solution for Google's Interview question: Check if any 2 numbers of an array add up to 8 (https://www.youtube.com/watch?v=XKu_SEDAykw)
def check_array_for_sum?(array, sum)
!!array.combination(2).detect { |a, b| a + b == sum }
end
sum = 8
array = [1, 2, 4, 9]
check_array_for_sum?(array, sum) # returns false
array = [1, 2, 4, 4]
check_array_for_sum?(array, sum) # returns true
array = [1, 2, 3, 6]
check_array_for_sum?(array, sum) # returns true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment