-
Notifications
You must be signed in to change notification settings - Fork 4
/
TWOSUM
28 lines (28 loc) · 823 Bytes
/
TWOSUM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
vector<int> twoSum(vector<int>v, int target) {
int n=v.size();
vector<int>vi=v;
sort(v.begin(),v.end());
vector<int>vec;
int hi=n-1;
int lo=0;
while(lo<hi){
int curr=v[lo]+v[hi];
if(curr==target){
int lo_pos= find(vi.begin(),vi.end(),v[lo])-vi.begin();
int hi_pos= find(vi.begin(),vi.end(),v[hi])-vi.begin();
if(lo_pos==hi_pos){
hi_pos= find(vi.rbegin(),vi.rend(),v[hi])-vi.rbegin();
}
vec.push_back(lo_pos);
vec.push_back(hi_pos);
return vec;
}
else if(curr<target){
lo++;
}
else {
hi--;
}
}
return vec;
}