Missing Numbers in java

Missing Numbers in java

Given two arrays of integers, find which elements in the second array are missing from the first array. 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 in ascending order. Only include a missing number once, even if it is missing multiple times.

Input Format

There will be four lines of input:

n – the size of the first array arr,

The next line contains n space-separated integers arr[i]

m – the size of the second array brr,

The next line contains m space-separated integers brr[i]

Output Format

Output array of integers (in increasing order) which tells you elements of second array not present in the first array

Example 1


203 204 205 206 207 208 203 204 205 206 
203 204 204 205 206 207 205 208 203 206 205 206 204


204 205 206


203 is in both arr and brr and its frequency is 3 in both arrays, hence it is not included in output

204 is included in both arrays but have different frequency hence, it is not included in output

Similarly other numbers are considered

Example 2


1 1 2 5
1 2 3 4


1 3 4


1 is in both arrays but frequency of 1 in both arrays is different, hence included in output

2 is in both arrays and have same frequency, hence not included in output

3 is in second array but not first, hence included in output

4 is in second array but not first, hence included in output

5 is in not in second array, hence not included in output


1 <= n, m <= 2 * 10^5

1 <= arr[i], brr[i] <= 10^4

Solution of Missing Numbers in java:–

import java.util.*;
import java.lang.*;
import java.io.*;

public class Main
	public static void main (String[] args) throws java.lang.Exception
		//your code here
                Scanner sc = new Scanner(System.in);
      int n = sc.nextInt();
      int A[] = new int[n];
      int fre1[] = new int[10001];
      for(int i=0;i<n;i++){
        A[i] = sc.nextInt();
      int m = sc.nextInt();
      int arrm[] = new int[m];
      int fre2[] = new int[10001];
      for(int i=0;i<m;i++){
        arrm[i] =  sc.nextInt();
      for(int i=0;i<10001;i++){
          System.out.print(i+" ");


Add a Comment

Your email address will not be published. Required fields are marked *