---
# System prepended metadata

title: '[Two Sum II](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/solution/)'
tags: [Leetcode, Two Pointers, Medium]

---

# [Two Sum II](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/solution/)
###### tags: `Leetcode`, `Medium`, `Two Pointers`

## Approach
* Initialize two pointers `left` and `right` to `0` and `len(nums) - 1`
* If sum of numbers at left and right index add up to sum; return `[nums[left], nums[right]]`
* If sum of numbers is < target; increment left by one
* If sum of numbers is > target; decrement right by one
* Return `[]`

## Asymptotic Analysis
### Time Complexity: **O(N)**
### Space Complexity: **O(1)**

## Code
``` python
from typing import List


class TwoSumII:

    @staticmethod
    def twoSum(numbers: List[int], target: int) -> List[int]:
        left, right = 0, len(numbers) - 1
        while left < right:
            current_sum = numbers[left] + numbers[right]
            if current_sum == target:
                return [left + 1, right + 1]
            elif current_sum > target:
                right -= 1
            else:
                left += 1
        return []


nums, t = [2, 3, 4], 6
print(TwoSumII.twoSum(nums, t))
```