-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind_All_Missing_Numbers.cpp
65 lines (53 loc) · 10.2 KB
/
Find_All_Missing_Numbers.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
/*
Problem Statement: We are given an unsorted array containing numbers taken from the range 1 to ‘n’. The array can have duplicates, which means some numbers will be missing. Find all those missing numbers.
Input: [2, 3, 1, 8, 2, 3, 5, 1]
Output: 4, 6, 7
Explanation: The array should have all numbers from 1 to 8, due to duplicates 4, 6, and 7 are missing.
*/
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
void swap(vector<int> &nums, int i, int j)
{
int temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
vector<int> find_all_missing_numbers(vector<int> &nums)
{
int i=0;
vector<int> result;
while(i<=nums.size())
{
if(nums[i]<=nums.size() && nums[i]!=nums[nums[i]])
{
swap(nums,i,nums[i]);
}
else
{
i++;
}
}
for(int i=1;i<=nums.size();i++)
{
if(i!=nums[i])
{
result.push_back(i);
}
}
return result;
}
int main()
{
vector<int> input={1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,13,1,5,2,1,3,9,5,5,12,13,24,12,12,13,14,12,13,14,12,13,12,14,13,12,48};
cout<<input.size();
for(auto res:find_all_missing_numbers(input))
{
cout<<res<<" ";
}
}
/*
Time Complexity : O(N) as we are swapping and iterating through the array for n times
O(N) (While loop )+ O(N-1)(swapping)+O(1)(i++)
Space Complexity is O(1)
*/