HackerRank Missing Numbers Problem Solution
In this post, we will solve HackerRank Missing Numbers Problem Solution.
Given two arrays of integers, find which elements in the second array are missing from the
first array.
Example
arr [7, 2, 5, 3, 5, 3] =
brr [7, 2, 5, 4, 6, 3, 5, 3]
The brr array is the orginal list. The numbers missing are [4, 6].
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.
- Return the missing numbers sorted ascending.
- Only include a missing number once, even if it is missing multiple times.
- The difference between the maximum and minimum numbers in the original list is less than or equal to 100.
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):
- int arr[n]: the array with missing numbers
- int brr[m]: the original array of numbers
Returns
- int[]: an array of integers
Input Format
There will be four lines of input:
n – the size of the first list, arr
The next line contains n space-separated integers arr[i]
m- the size of the second list, brr
The next line contains m space-separated integers brr[i]
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
204 is present in both arrays. Its frequency in arr is 2, while its frequency in brr is 3. Similarly, 205 and 206 occur twice in arr, but three times in brr. The rest of the numbers
have the same frequencies in both lists.
Missing Numbers C Solution
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int key[100];
for(int i=0;i<100;i++)
key[i]=0;
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int m;
scanf("%d",&m);
int b[m];
scanf("%d",&b[0]);
int min = b[0] ;
for(int i=1;i<m;i++)
{
scanf("%d",&b[i]);
if(b[i]<=min)
min = b[i];
}
int zero=0,z;
for(int i=0;i<n;i++)
{
key[a[i]-min]-=a[i];
if(a[i]==0)
{
zero++;
}
}
for(int i=0;i<m;i++)
{
key[b[i]-min]+=b[i];
if(b[i]==0)
{
zero--;
z=b[i]-min;
}
}
for(int i=0;i<100;i++)
{
if(key[i]!=0)
{
printf("%d ",(i+min));
}
else if(i==z&&zero!=0)
printf("%d ",key[i]);
}
return 0;
}
Missing Numbers C++ Solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#define SIZE 10001
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int numbers [SIZE] ;
for(int i=0; i<SIZE; i++){
numbers[i] = 0 ;
}
int n,m,x;
cin>>n;
for(int i=0; i<n; i++){
cin>>x;
//cerr<<x<<" ";
numbers[x]--;
}
//cerr<<endl;
cin>>m;
for(int i=0; i<m; i++){
cin>>x;
//cerr<<x<<" ";
numbers[x]++;
}
for(int i=0; i<SIZE; i++){
if(numbers[i]>0)
cout<<i<<" ";
}
return 0;
}
Missing Numbers C Sharp Solution
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hackerrank
{
class Solution
{
public static void printFindMissingNumbers(List<int> data1, List<int> data2)
{
byte[] counts1 = new byte[100];
byte[] counts2 = new byte[100];
int min = int.MaxValue;
for (int i = 0; i < data1.Count; i++)
{
if (data1[i] < min)
{
min = data1[i];
}
}
for (int i = 0; i < data1.Count; i++)
{
counts1[data1[i] - min]++;
}
for (int i = 0; i < data2.Count; i++)
{
counts2[data2[i] - min]++;
}
List<int> results = new List<int>();
for (int i = 0; i < 100; i++)
{
if (counts2[i] != 0 && counts1[i] != counts2[i])
{
System.Console.Write(String.Format("{0} ", i + min));
}
}
}
public static void Main(string[] args)
{
int n = int.Parse(System.Console.ReadLine());
string stringBuffer = System.Console.ReadLine();
string[] stringArrayData = stringBuffer.Split(' ');
List<int> data1 = new List<int>(n);
for (int i = 0; i < n; i++)
{
data1.Add(int.Parse(stringArrayData[i]));
}
n = int.Parse(System.Console.ReadLine());
stringBuffer = System.Console.ReadLine();
stringArrayData = stringBuffer.Split(' ');
List<int> data2 = new List<int>(n);
for (int i = 0; i < n; i++)
{
data2.Add(int.Parse(stringArrayData[i]));
}
printFindMissingNumbers(data1, data2);
}
public static void MainTest(string[] args)
{
string[] inputData = System.IO.File.ReadAllLines(args[0]);
int n = int.Parse(inputData[0]);
string[] stringArrayData = inputData[1].Split(' ');
List<int> data = new List<int>(n);
for (int i = 0; i < n; i++)
{
data.Add(int.Parse(stringArrayData[i]));
}
printFindMissingNumbers(data, data);
}
}
}
Missing Numbers Java Solution
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'missingNumbers' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts following parameters:
* 1. INTEGER_ARRAY arr
* 2. INTEGER_ARRAY brr
*/
public static List<Integer> missingNumbers(List<Integer> arr, List<Integer> brr) {
int[] bucket = new int[201];
for(int i = 0; i < bucket.length; i++) {
bucket[i] = 0;
}
bucket[100] = 1;
Function<Integer, Integer> getIndex = value -> value - arr.get(0) + 100;
for(int i = 1; i < arr.size(); i++) {
bucket[getIndex.apply(arr.get(i))]++;
}
List<Integer> missingNumbers = new ArrayList();
for(int i = 0; i < brr.size(); i++) {
int index = getIndex.apply(brr.get(i));
bucket[index]--;
}
for(int i = 0; i < bucket.length; i++) {
if(bucket[i] != 0) missingNumbers.add(i - 100 + arr.get(0));
}
return missingNumbers;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
int m = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> brr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
List<Integer> result = Result.missingNumbers(arr, brr);
bufferedWriter.write(
result.stream()
.map(Object::toString)
.collect(joining(" "))
+ "\n"
);
bufferedReader.close();
bufferedWriter.close();
}
}
Missing Numbers JavaScript Solution
function processData(input) {
var dataArray = input.split("\n")
var lengthA = parseInt(dataArray[0], 10)
var dataA = dataArray[1].split(' ').map(function(v) { return parseInt(v, 10)})
var lengthB = parseInt(dataArray[2], 10)
var dataB = dataArray[3].split(' ').map(function(v) { return parseInt(v, 10)})
var listC = {}
dataB.forEach(function(value) {
if(!listC[value]) {
listC[value] = 1
} else {
listC[value]++
}
})
dataA.forEach(function(value) {
if(listC[value]) {
listC[value]--
if(listC[value] === 0) {
delete listC[value]
}
}
})
console.log(Object.keys(listC).join(' '))
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
Missing Numbers Python Solution
from collections import Counter
if __name__ == '__main__':
a = int(input())
A = list(map(int, input().strip().split(" ")))
b = int(input())
B = list(map(int, input().strip().split(" ")))
counterA = Counter(A)
counterB = Counter(B)
missing = []
for key, value in counterB.items():
if value > counterA[key]:
missing.append(key)
for element in missing:
print(element, end=' ')