【HackerRank】Missing Numbers

Numeros, The Artist, had two lists A and B, such that, B was a permutation of A. Numeros was very proud of these lists. Unfortunately, while transporting them from one exhibition to another, some numbers from List A got left out. Can you find out the numbers missing from A?

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 list B is less than or equal to 100.

Input Format 
There will be four lines of input:

n - the size of the first list 
This is followed by n space separated integers that make up the first list. 
m - the size of the second list 
This is followed by m space 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



题解:设置两个数组,因为x的范围在1~10000之间,只要开两个10001的数组分别记录A和B中元素的个数,然后比较两个数组就可以了。

代码如下:

 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 }

【HackerRank】Missing Numbers

时间: 2024-10-24 01:15:01

【HackerRank】Missing Numbers的相关文章

【HackerRank】Closest Numbers

Sorting is often useful as the first step in many different tasks. The most common task is to make finding things easier, but there are other uses also. Challenge Given a list of unsorted numbers, can you find the numbers that have the smallest absol

【HackerRank】QuickSort(稳定快排,空间复杂度O(n))

QuickSort In the previous challenge, you wrote a partition method to split an array into 2 sub-arrays, one containing smaller elements and one containing larger elements. This means you 'sorted' half the array with respect to the other half. Can you

【HackerRank】Find the Median(Partition找到数组中位数)

In the Quicksort challenges, you sorted an entire array. Sometimes, you just need specific information about a list of numbers, and doing a full sort would be unnecessary. Can you figure out a way to use your partition code to find the median in an a

【HackerRank】Service Lane

Calvin is driving his favorite vehicle on the 101 freeway. He notices that the check engine light of his vehicle is on, and he wants to service it immediately to avoid any risks. Luckily, a service lane runs parallel to the highway. The length of the

【HackerRank】Running Time of Quicksort

题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than Insertion Sort? Compare the running time of the two algorithms by counting how many swaps or shifts each one takes to sort an array, and output the dif

【HackerRank】Median

题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大堆存放前半部分较小的元素,最小堆存放后半部分较大的元素,并且最大堆的所有元素小于最小堆的所有元素:保持最大堆最多比最小堆多一个元素.每次插入元素的时候都先插入到最大堆,如果发现最大堆比最小堆多了两个个,那么就从最大堆里面拿出最大的放到最小堆里面:如果发现最大堆里面新插入的元素破坏了最大堆所有元素小于

【HackerRank】Game Of Rotation

题目连接:Game Of Rotation Mark is an undergraduate student and he is interested in rotation. A conveyor belt competition is going on in the town which Mark wants to win. In the competition, there's A conveyor belt which can be represented as a strip of 1

【HackerRank】Bus Station

有n组好朋友在公交车站前排队.第i组有ai个人.还有一辆公交车在路线上行驶.公交车的容量大小为x,即它可以同时运载x个人. 当车站来车时(车总是空载过来),一些组从会队头开始走向公交车. 当然,同一组的朋友不想分开,所以仅当公交车能容纳下整个组的时候,他们才会上车.另外,每个人不想失去自己的位置,即组的顺序不会改变. 问题时如何选择公交车的容量大小x使得它可以运走所有组的人,并且公交车每次从车站出发时没有空位?(在车里的总人数恰好达到x)? 输入格式 第一行只包含一个整数n.第二行包含n个空格分

【HackerRank】Sherlock and MiniMax

题目连接:Sherlock and MiniMax Watson gives Sherlock an array A1,A2...AN. He asks him to find an integer M between P and Q(both inclusive), such that, min {|Ai-M|, 1 ≤ i ≤ N} is maximised. If there are multiple solutions, print the smallest one. Input For