Last active
August 29, 2015 14:08
-
-
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.
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 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