-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindMissingNumbers.cpp
58 lines (37 loc) · 1.08 KB
/
FindMissingNumbers.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
/*Hi, here's your problem today. This problem was recently asked by Twitter:
Given an array of integers of size n, where all elements are between 1 and n inclusive, find all of the elements of [1, n] that do not appear in the array. Some numbers may appear more than once.
Example:
Input: [4,5,2,6,8,2,1,5]
Output: [3,7]
*/
#include <iostream>
#include <algorithm>
#include <vector>
std::vector<int> missing_num(int *arr,int n)
{
std::sort(arr,arr+n);
//main logic
std::vector<int> list;
for (int i = 0; i < n-1; i++)
{
if (arr[i+1]!=arr[i] and arr[i+1]!=arr[i]+1)
{
list.push_back(arr[i]+1); //pushing the current element plus 1
}
}
return list;
}
int main()
{
int n = 8;
int arr[n]{4,5,2,6,8,2,1,5};
int arrs[7]{4,6,2,6,7,2,1};
std::vector<int> missing_elems = missing_num(arr,n);
std::vector<int> missing_elems2 = missing_num(arrs,7);
for (auto i : missing_elems2)
{
std::cout<<i<<" ";
}
return 0;
}
// ^ check later if it cane be optimized further