Created
June 16, 2017 21:42
-
-
Save evidanary/533c5461b1cd3f37270de6750f9d4b00 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
| # 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