Created
February 15, 2016 23:50
-
-
Save toshihirock/6ddba841a1540e1fb57b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# O(n) | |
def binarytree(array, target) | |
right = array[0] | |
left = array[array.length - 1] | |
while right != left do | |
sum = right + left | |
return "find" if sum == target | |
if sum > target | |
left = left - 1 | |
else | |
right = right + 1 | |
end | |
end | |
"none" | |
end | |
# main | |
array = [5, 2, 0, 9, 6, 1, 3, 4] | |
target = 9 | |
# sort is O(n log n) | |
puts binarytree(array.sort, target) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment