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

Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

View Solution →

Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

View Solution →

Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

View Solution →

Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

View Solution →

Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

View Solution →

Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →