Missing Numbers


Problem Statement :


Given two arrays of integers, find which elements in the second array are missing from the first array.

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:

 - 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



Solution :



title-img


                            Solution in C :

In   C++   :






#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;


int main() {
  
    long long n,m,temp;
    cin>>n;
    vector<int> a;
    for(long long i=0;i<n;i++) {
        cin >> temp;
        a.push_back(temp);
    }
    cin>>m;
    vector<int> b;
    for(long long i=0;i<m;i++){
        cin >> temp;
        b.push_back(temp);
    }
    sort(a.begin(),a.end());
    sort(b.begin(),b.end());
    long long i=0,j=0;
    while(i<n && j<m){
        if(a[i]==b[j]) {
            i++;j++;
            b[j-1]=0;
        }
        else if(a[i]>b[j])j++;
        else i++;
    }
    set<int> st;
    for(i=0;i<m;i++) {
        if(b[i]!=0) st.insert(b[i]);
    }
    for(set<int>::iterator it = st.begin();it!=st.end();it++){
        cout<<*it<<" ";
    }
    cout<<endl;
    return 0;
}









In   Java :







import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static void ans(int a[],int b[]){
        int count_array1[] = new int[101];
        int count_array2[] = new int[101];
        int min = min(b);
        int max = max(b);
        int diff = max - min;
        for(int i=0;i<a.length;i++){
            count_array1[a[i]-min]++;
        }
        for(int i=0;i<b.length;i++){
            count_array2[b[i]-min]++;
        }
        for(int i=0;i<=100;i++){
            int diff_count = count_array2[i] - count_array1[i];
            if(diff_count > 0){
                System.out.print((i+min) + " ");
            }
        
        }
    }
    static int min(int a[]){
       int min = a[0];
        for(int i=1;i<=a.length-1;i++){
            if(a[i] < min){
                min = a[i];
            }
        }
        return min;
    }
    static int max(int b[]){
        int max = b[0];
        for(int i=1;i<=b.length-1;i++){
            if(b[i] > max){
                max = b[i];
            }
        }
        return max;
    }
    public static void main(String[] args) {
       Scanner s = new Scanner(System.in);
       int N,M;
       N = s.nextInt();
       int array1[] = new int[N];
       for(int i=0;i<=N-1;i++){
            array1[i] = s.nextInt();
       }
       M = s.nextInt();
       int array2[] = new int[M];
       for(int i=0;i<=M-1;i++){
            array2[i] = s.nextInt();
       } 
       ans(array1,array2); 
        
    }
}









In  C  :








#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int cnt[22222];

int main() {
    memset(cnt,0,sizeof(cnt));
    int n;
    scanf("%d",&n);
    for(int i=0,x;i<n;i++){
        scanf("%d",&x);
        cnt[x+10000]--;
    }
    scanf("%d",&n);
    for(int i=0,x;i<n;i++){
        scanf("%d",&x);
        cnt[x+10000]++;
    }
    for(int i=-10000;i<=10000;i++)
        if(cnt[i+10000] > 0)
            printf("%d ",i);
    return 0;
}









In   Python3 :






n = int(input())
A = [int(i) for i in input().split()]
m = int(input())
B = [int(i) for i in input().split()]
vals = {}
for i in A:
    if i not in vals:
        vals[i] = -1
    else:
        vals[i] -= 1
for i in B:
    if i not in vals:
        vals[i] = 1
    else:
        vals[i] += 1
pos = []
for i in vals:
    if vals[i] > 0:
        pos.append(i)
pos.sort()
for i in pos:
    print(i,end=' ')
                        








View More Similar Problems

Cube Summation

You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor

View Solution →

Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →

Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →

Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →

Mr. X and His Shots

A cricket match is going to be held. The field is represented by a 1D plane. A cricketer, Mr. X has N favorite shots. Each shot has a particular range. The range of the ith shot is from Ai to Bi. That means his favorite shot can be anywhere in this range. Each player on the opposite team can field only in a particular range. Player i can field from Ci to Di. You are given the N favorite shots of M

View Solution →

Jim and the Skyscrapers

Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space

View Solution →