Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SWIFT

two sum problem

vector<int> twoSum(vector<int>& nums, int target) {
    int front = 0, rear = nums.size() - 1;
    sort(nums.begin(), nums.end());
    while(front < rear) {
      int sum = nums[front] + nums[rear];
      if (sum == target)
       break;
      else if (sum > target)
       rear--;
      else
       front++;
    }
    return {front, rear};
};
Source by shareablecode.com #
 
PREVIOUS NEXT
Tagged: #sum #problem
ADD COMMENT
Topic
Name
1+6 =