forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path167.cpp
More file actions
25 lines (23 loc) · 686 Bytes
/
167.cpp
File metadata and controls
25 lines (23 loc) · 686 Bytes
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
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int i,j, n= numbers.size();
vector<int> res(2,-1);
i=0;j=n-1;
while (i<j){
//sum equal
if (numbers[i]+numbers[j]==target){
res[0]=i+1;res[1]=j+1;
return res;
}
else if (numbers[i]+numbers[j]>target)
j--;
else
i++;
}
return res;
}
};
//==============================================================
//Time complexity of the above algorith: O(n), n is the length of the input array
//Space complexity: O(1), constant space