#Leecode #CodingChallenge #Algorithm

Leetcode 167. Two Sum II - Input Array Is Sorted

A very clever solution that uses two pointers (indices) from the start and the end.

Compare the sum with the "target" and this naturally returns the two indices.

Compare the sum with the "target" and this naturally returns the two indices.

class Solution(object): def twoSum(self, numbers, target): l, r = 0, len(numbers) - 1 #two indices from start and end while l < r: curSum = numbers[l] + numbers[r] if curSum > target: r -= 1 elif curSum < target: l += 1 else: return [l + 1, r + 1] #source: neetcode