---
# System prepended metadata

title: 167-Two Sum II - Input Array Is Sorted
tags: [Easy]

---

# 167-Two Sum II - Input Array Is Sorted
###### tags: `Easy`
## Question
https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
## Key
1. Two pointers
## Reference

## Solution
```cpp=
class Solution {
public:
  vector<int> twoSum(vector<int>& nums, int target) {
    if(nums.size() < 2) return {};
    int l = 0, r = nums.size()-1;

    while(l <= r) {
      if(nums[l] + nums[r] == target) {
        // index start from 1, so we need to add 1
        return {l+1, r+1};
      }
      else if(nums[l] + nums[r] > target) r--;
      else if(nums[l] + nums[r] < target) l++;
    }

    return {};
  }
};
```
