forked from ianshulx/DSA-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Two_Sum.cpp
40 lines (33 loc) · 918 Bytes
/
Two_Sum.cpp
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
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map<int,int> m;
vector<int> v;
int n= nums.size();
for(int i=0;i<n;i++)
{
int diff = target - nums[i];
if(m.find(diff) != m.end())
{
auto p = m.find(diff);
v.push_back(p->second);
v.push_back(i);
}
m.insert(make_pair(nums[i],i));
}
return v;
}
};
int main(){
Solution s ;
vector<int> v;
v={1,6,3,2,5};
vector<int> result= s.twoSum (v, 11);
for(int i: result)
{
cout<<i<<endl;
}
return 0;
}