Skip to content

Instantly share code, notes, and snippets.

@evidanary
Created June 16, 2017 21:42
Show Gist options
  • Select an option

  • Save evidanary/533c5461b1cd3f37270de6750f9d4b00 to your computer and use it in GitHub Desktop.

Select an option

Save evidanary/533c5461b1cd3f37270de6750f9d4b00 to your computer and use it in GitHub Desktop.
# check if there is two integers which sum up to the targeted sum
def two_sum(arr, target)
arr.sort!
size = arr.size
i, j = 0, size-1
count = 0
while(i<j) do
sum = arr[i] + arr[j]
if sum == target
count += 1
i+=1
elsif sum < target
i += 1
else
j -= 1
end
end
count
end
puts two_sum([0,1,2,3,4,5], 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment