Hello guys!
I'm kind of a "new kid" in this c++ thing, so I'm trying to learn things trough some tasks and answers.. (today trough hackerrank site)
So, first task is to find missing numbers, and the notes are:
-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
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
|
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
#include <map>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n,m,A[1000010];
map <int,int> mp;
int main()
{
scanf("%d",&n);
for (int i=0;i<n;i++)
scanf("%d",&A[i]);
scanf("%d",&m);
int tmp;
while (m--)
{
scanf("%d",&tmp);
mp[tmp]++;
}
for (int i=0;i<n;i++)
mp[A[i]]--;
for (map<int,int> :: iterator j = mp.begin();j!=mp.end();j++)
{
if (j->second > 0)
printf("%d ",j->first);
}
}
|
I ask you guys kindly to explain me this code step by step (or line by line), so that I could understand what's happening where..
Thanks in advance!