/
missingNumbers.cpp
69 lines (65 loc) · 1.99 KB
/
missingNumbers.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
/**
* Problem Statement
*
* Numeros, the Artist, had two lists A and B, such that B was a permutation of A.
* Numeros was very proud of these lists. Unfortunately, while transporting them from one exhibition to another, some numbers were left out of A.
* Can you find the missing numbers?
*
* Notes
*
* If a number occurs multiple times in the lists, you must ensure that the frequency of that number in both lists is the same.
* If that is not the case, then it is also a missing number.
* You have to print all the missing numbers in ascending order.
* Print each missing number once, even if it is missing multiple times.
* The difference between maximum and minimum number in B is less than or equal to 100.
* Input Format
* There will be four lines of input:
*
* n - the size of the first list
* This is followed by n space-separated integers that make up the first list.
* m - the size of the second list
* This is followed by m space-separated integers that make up the second list.
*
* Output Format
* Output the missing numbers in ascending order.
*/
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
int main() {
long n,m;
cin >> n;
long *list1 = new long[n];
for (long i = 0; i < n; ++i) {
cin >> list1[i];
}
cin >> m;
long *list2 = new long[m];
for (long i = 0; i < m; ++i) {
cin >> list2[i];
}
unordered_map<long,long> hash;
for (long i = 0; i < m; ++i) {
hash[list2[i]]++;
}
for (long i = 0; i < n; ++i) {
hash[list1[i]]--;
}
unordered_map<long,long>::iterator it;
vector<long> results;
for (it = hash.begin(); it != hash.end(); ++it){
if ( it->second > 0) {
results.push_back(it->first);
}
}
sort(results.begin(), results.end());
vector<long>::iterator i;
for (i = results.begin(); i != results.end(); ++i){
cout << *i << " ";
}
return 0;
}