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

Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

View Solution →

Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

View Solution →

Down to Zero II

You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.

View Solution →

Truck Tour

Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr

View Solution →

Queries with Fixed Length

Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon

View Solution →

QHEAP1

This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element

View Solution →