Advertisement
Josif_tepe

Untitled

Jan 20th, 2023
960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     vector<int> twoSum(vector<int>& nums, int target) {
  4.         vector<pair<int, int> > v;
  5.         for(int i = 0; i < nums.size(); i++) {
  6.             v.push_back(make_pair(nums[i], i));
  7.         }
  8.         sort(v.begin(), v.end());
  9.  
  10.         int S = 0;
  11.         int E = nums.size() - 1;
  12.         while(S < E) {
  13.             if(v[S].first + v[E].first == target) {
  14.                 return {v[S].second, v[E].second};
  15.             }
  16.             else if(v[S].first + v[E].first > target) {
  17.                 E--;
  18.             }
  19.             else {
  20.                 S++;
  21.             }
  22.         }
  23.         return {};
  24.     }
  25. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement