-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindAllTheNumvers.cpp
68 lines (65 loc) · 1.51 KB
/
findAllTheNumvers.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
64
65
66
67
68
// find all the number disapear in the array
// input = [4,3,2,7,8,2,3,1] n = 8
// output = [5,6]
#include<bits/stdc++.h>
using namespace std;
// class Solution
// {
// public:
// vector<int> findAllNumbers(vector<int> arr)
// {
// int n = arr.size();
// map<int, int> mpp;
// vector<int> res;
// for(int i = 0; i< n; i++)
// {
// mpp[arr[i]] += 1;
// }
// for(int i = 1; i< n+1; i++)
// {
// if(mpp[i] == 0)
// {
// res.push_back(i);
// }
// }
// return res;
// }
// };
// this is the better solution whose time complexity is o(nlogn)+o(n)+o(n)
class Solution
{
public:
vector<int> findAllNumbers(vector<int> arr)
{
int n = arr.size();
vector<int> res;
for(int i = 0; i< n; i++)
{
int idx = abs(arr[i]) -1;
if(arr[idx] > 0)
{
arr[idx] = -arr[idx];
}
}
for(int i = 0; i< n; i++)
{
if(arr[i] > 0)
{
res.push_back(i+1);
}
}
return res;
}
};
// this is the optimal solution whose time complexity is o(2n). and space complexity is o(1).
int main()
{
vector<int> v = {4,3,2,7,8,2,3,1};
Solution s;
vector<int> a = s.findAllNumbers(v);
for(auto it : a)
{
cout<< it<< " ";
}
return 0;
}