LC 167. Two Sum II - Input array is sorted

Nilanjan Deb · April 5, 2020

This is my CPP solution.

class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
        int s = 0, e = numbers.size()-1; 
        vector<int>x;
        while(s<e){
            if(numbers[s] + numbers[e] == target){
                x.push_back(s+1);
                x.push_back(e+1);
                break;
            }
            else if(numbers[s] + numbers[e] < target)
                s = s+1;
            else
                e = e-1;
        } 
        return x;
    }
};


Dicussion Forum