Skip to content

Instantly share code, notes, and snippets.

@toshihirock
Created February 15, 2016 23:50
Show Gist options
  • Save toshihirock/6ddba841a1540e1fb57b to your computer and use it in GitHub Desktop.
Save toshihirock/6ddba841a1540e1fb57b to your computer and use it in GitHub Desktop.
# 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