-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwoSumElements.cpp
63 lines (60 loc) · 1.4 KB
/
twoSumElements.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<bits/stdc++.h>
using namespace std;
// class Solution
// {
// public:
// vector <int> twoSumElements(vector <int> arr, int k)
// {
// int n = arr.size();
// vector <int> v;
// for(int i = 0; i< n; i++)
// {
// for(int j = i+1; j< n; j++)
// {
// int sum = arr[i] + arr[j];
// if(sum == k)
// {
// v.push_back(arr[i]);
// v.push_back(arr[j]);
// break;
// }
// }
// }
// return v;
// }
// };
// the time complexty is o(n^2).
class Solution
{
public:
vector <int> twoSumElements(vector <int> arr, int k)
{
map <int, int> m;
vector <int> v;
int more = 0;
for(int i = 0; i< arr.size(); i++)
{
more = k - arr[i];
if(m.find(more) != m.end())
{
v.push_back(arr[i]);
v.push_back(more);
}
m[arr[i]] = more;
}
return {-1, -1};
}
};
// the time complexity of this code is o(nlogN)+o(n).
int main()
{
vector <int> v = {2, 6, 5, 8, 11};
int k = 9;
Solution s;
vector <int> a = s.twoSumElements(v, k);
for(auto p : a)
{
cout<< p<< " ";
}
return 0;
}