We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3fdd758 commit 5cb03adCopy full SHA for 5cb03ad
CPP/arrays/Two sum 2/twoSum.cpp
@@ -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