Created
March 15, 2017 15:27
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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