[Hackerrank] missing numbers

Source: Internet
Author: User

Numeros, the artist, had two listsAAndB, Such that,BWas a permutationA. Numeros was very proud of these lists. Unfortunately, while transporting them from one exhibition to another, some numbers from ListAGot left out. Can you find out the numbers missing fromA?

Notes

  • If a number occurs multiple times in the lists, you must ensure that the frequency of that number in both the lists is the same. if that is not the case, then it is also a missing number.
  • You have to print all the missing numbers in ascending order.
  • Print each missing number once, even if it is missing multiple times.
  • The difference between maximum and minimum number in the listBIs less than or equal to 100.

Input Format 
There will be four lines of input:

N-The size of the first list
This is followedNSpace separated integers that make up the first list.
M-The size of the second list
This is followedMSpace separated integers that make up the second list.

Output Format 
Output the missing numbers in ascending order

Constraints 
1 <=N, m<= 1000010
1 <=X<= 10000,Xε B 
Xmax-Xmin<101

 

Question: set two arrays because the range of X is 1 ~ Between 10000, you only need to open two 10001 arrays to record the number of elements in A and B respectively, and then compare the two arrays.

The Code is as follows:

 1 import java.util.*; 2  3 public class Solution { 4     public static void main(String[] args) { 5         Scanner in = new Scanner(System.in); 6         int[] CountA = new int[10005]; 7         int[] CountB = new int[10005]; 8          9         int n = in.nextInt();10         int[] a = new int[n];11         for(int i = 0;i < n;i++){12             a[i]=in.nextInt();13             CountA[a[i]]++;14         }15         16         int m = in.nextInt();17         int[] b = new int[m];18         for(int i = 0;i < m;i++){19             b[i]=in.nextInt();20             CountB[b[i]]++;21         }22         23         for(int i = 1;i <= 10000;i++){24             if(CountB[i]>CountA[i] )25                 System.out.printf("%d ", i);26         }27         System.out.println();28         29         30         31     }32 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.