Skip to content

Instantly share code, notes, and snippets.

@mosdevly
Last active August 29, 2015 14:08
Show Gist options
  • Save mosdevly/8823790ac50bcb05dca8 to your computer and use it in GitHub Desktop.
Save mosdevly/8823790ac50bcb05dca8 to your computer and use it in GitHub Desktop.
Given an array of integers, find two numbers such that they add up to a specific number. The solution twosum should return indices of the two numbers, where index1 must be less than index2. The returned number shouldn't be zero based. Assume that each input would have exactly one solution.
def twosum nums
idx = 0
idx2 = idx + 1
while idx < nums.length
n = nums[idx]
i = nums[idx2]
sum = n + i
if sum == 0
puts [idx, idx2]
end
idx += 1
end
end
twosum [1,3,5,-3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment