-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path167.TwoSumII_InputArrayIsSorted.cpp
More file actions
51 lines (46 loc) · 1017 Bytes
/
167.TwoSumII_InputArrayIsSorted.cpp
File metadata and controls
51 lines (46 loc) · 1017 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<bits/stdc++.h>
using namespace std;
class Solution{
public:
vector<int> twoSum(vector<int> &numbers,int target)
{
vector<int> targetArray;
int start=0,end=numbers.size()-1,sum;
while(start<end)
{
sum=numbers[start]+numbers[end];
if(sum==target){
targetArray.push_back(start+1);
targetArray.push_back(end+1);
return targetArray;
}
else if(sum<target){
start++;
}
else{
end--;
}
}
return {};
}
};
int main()
{
int nums,target;
vector<int> numbers,targetArray;
cout<<"Enter the elements :"<<endl;
while(cin>>nums)
{
numbers.push_back(nums);
}
cout<<"Enter the target"<<endl;
cin>>target;
Solution solution;
targetArray=solution.twoSum(numbers,target);
for(int i=0;i<targetArray.size();i++)
{
i==targetArray.size()-2?cout<<targetArray[i]<<" , ":cout<<targetArray[i]<<" ";
}
cout<<endl;
return 1;
}