Skip to content

Commit 5cb03ad

Browse files
Two sum 2
1 parent 3fdd758 commit 5cb03ad

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

CPP/arrays/Two sum 2/twoSum.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include<iostream>
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
vector<int> twoSum(vector<int>& numbers, int target) {
5+
vector<int> res;
6+
int n = numbers.size();
7+
int low = 0,high = n - 1;
8+
while(low<high){
9+
int sum = numbers[low] + numbers[high];
10+
if(sum>target)
11+
high -= 1;
12+
else if(sum<target)
13+
low += 1;
14+
else{
15+
res.push_back(low+1);
16+
res.push_back(high+1);
17+
break;
18+
}
19+
}
20+
return res;
21+
}
22+
int main(){
23+
24+
vector<int> numbers({2,3,4});
25+
int target = 6;
26+
vector<int> res = twoSum(numbers,target);
27+
for(auto x:res)
28+
cout<<x<<endl;
29+
return 0;
30+
}

0 commit comments

Comments
 (0)