Hackerrank - Missing Numbers Solution
1 min read

Hackerrank - Missing Numbers Solution

Hackerrank - Missing Numbers Solution

Numeros the Artist had two lists that were permutations of one another. He was very proud. Unfortunately, while transporting them from one exhibition to another, some numbers were lost out of the first list. Can you find the missing numbers?

As an example, the array with some numbers missing, . The original array of numbers . The numbers missing are .

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 the second list is less than or equal to .

Function Description

Complete the missingNumbers function in the editor below. It should return a sorted array of missing numbers.

missingNumbers has the following parameter(s):

  • arr: the array with missing numbers
  • brr: the original array of numbers

Input Format

There will be four lines of input:

- the size of the first list,
The next line contains  space-separated integers
- the size of the second list,
The next line contains  space-separated integers

Constraints

Output Format

Output the missing numbers in ascending order.

Sample Input

10
203 204 205 206 207 208 203 204 205 206
13
203 204 204 205 206 207 205 208 203 206 205 206 204

Sample Output

204 205 206

Explanation

is present in both arrays. Its frequency in  is , while its frequency in  is . Similarly,  and  occur twice in , but three times in . The rest of the numbers have the same frequencies in both lists.

Solution in Python

from collections import Counter
def missingNumbers(arr, brr):
    a = [x for x,y in brr.items() if y-arr.get(x,0)]
    return sorted(a)
n,arr = input(), Counter(map(int,(input().split())))
m,brr = input(), Counter(map(int,(input().split())))
print(*missingNumbers(arr, brr))

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×