Last active
September 15, 2015 04:02
-
-
Save kartikkukreja/eb353e611a3002060047 to your computer and use it in GitHub Desktop.
2Sum in sorted array
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
def twoSumSortedArray(A, k): | |
""" | |
Given a sorted integer array A, return two elements from it which sum to k | |
""" | |
start, end = 0, len(A) - 1 | |
while start != end: | |
if A[start] + A[end] == k: return (A[start], A[end]) | |
elif A[start] + A[end] > k: end -= 1 | |
else: start += 1 | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment