【HackerRank】Missing Numbers
2014-08-15 12:50
309 查看
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中元素的个数,然后比较两个数组就可以了。
代码如下:
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中元素的个数,然后比较两个数组就可以了。
代码如下:
import java.util.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int[] CountA = new int[10005]; int[] CountB = new int[10005]; int n = in.nextInt(); int[] a = new int ; for(int i = 0;i < n;i++){ a[i]=in.nextInt(); CountA[a[i]]++; } int m = in.nextInt(); int[] b = new int[m]; for(int i = 0;i < m;i++){ b[i]=in.nextInt(); CountB[b[i]]++; } for(int i = 1;i <= 10000;i++){ if(CountB[i]>CountA[i] ) System.out.printf("%d ", i); } System.out.println(); } }
相关文章推荐
- 【Hackerrank】Find the merge point of two joined linked lists
- 【HackerRank】 The Full Counting Sort
- [hackerrank] 结构化字符串(解析树)
- HackerRank - Max Min
- HackerRank - Play with words
- HackerRank - "Fraud Prevention"
- Hackerrank-Data structures-Tree
- HackerRank - "Candles Counting" - DIFFICULT
- [hackerrank] booking.com
- HackerRank "Minimum Average Waiting Time" !
- Hackerrank:Yet Another KMP Problem
- Short Palindrome[HackerRank]
- [HackerRank University CodeSprint]Unique Divide And Conquer
- Python HackerRank|DefaultDict Tutorial(from collections import defaultdict)
- HackerRank--Stock Maximize
- hackerrank:Text Wrap
- C++ HackerRank|Waiter
- 【Hackerrank 101Hack 43】【JZOJ5135】K-Inversion Permutations 题解
- hackerrank>Dashboard>C++>STL>Vector-Erase
- HackerRank Pattern Count 题解