Missing numbers

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!

Last edited on
Morning.


I'm kind of a "new kid" in this c++ thing

Are you wanting to learn C, or C++, because if it's C++, then the above code will teach you some 'nasty' (ish) habits.

scanf and printf are C functions (which you can use in C++, but there's nicer constructs:
http://www.cplusplus.com/doc/tutorial/basic_io/
).

scanf is is reading stuff in to your variables from the user (into array elements in the first case, and then your variable m in the second case)

lines 21 to 25 are doing a similar sort of thing but populating a c++ map.
Last edited on
First, I highly dislike the style of the program, C++ programs should use vectors instead of arrays and cin/cout instead of printf/scanf.

The program also contains an error which makes its solution not entirely correct.

And finally, given your original question it is my opinion that this program is too complex for you (uses iterators and maps). So instead of trying to figure it out, try finding some tutorial/excercises that are more appropriate for your level of knowledge.

Edit: Tutorials should really give better written solution than the one above. Find a better tutorial. I won't discuss this terrible solution.
Last edited on
Topic archived. No new replies allowed.