167. Two Sum II - Input Array Is SortedΒΆ
Similar to problem TwoSum, except the nums array is sorted now.
We can greedily use this information by comparing sum of left and right end. If our sum exceed target,
we should reduce it by decreasing the right pointer. Else if our sum is smaller than target, we can increase
our current sum by increasing left pointer.
Runtime Complexity
Time: \(O(n)\), since we're only iterating the nums array once.
Space: \(O(1)\), constant space from two pointers.