Two Sum II | LeetCode

 Two Sum II


Question with Examples



The solution to the Question-


Solution to the above Question

class Solution {

public:

    vector<int> twoSum(vector<int>& nums, int target) {

        int start=0;

        vector<int>v;

        int end=nums.size()-1;

        int sum=0;

        while(end>start){

            sum=nums[start]+nums[end];

            if(sum>target){

                end--;

            }

            if(sum<target){

                start++;

            }

            if(sum==target){

                v.push_back(start+1);

                v.push_back(end+1);

                return v;

            }

        }

        return v;

    }

};

Post a Comment

0 Comments