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.
https://sisijava.wordpress.com/2014/07/23/hackerrank-missing-numbers/
Using hashmap.
Actually we just need one hashmap.
http://aruncyberspace.blogspot.com/2014/04/algorithms-search-missing-numbers.html
// No point to sort a,b first
https://codepair.hackerrank.com/paper/keonZBPs?b=eyJyb2xlIjoiY2FuZGlkYXRlIiwibmFtZSI6ImplZmZlcnl5dWFuIiwiZW1haWwiOiJ5dWFueXVuLmtlbm55QGdtYWlsLmNvbSJ9
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
final int XMAX = 10000;
final int RANGE_SIZE = 101;
Scanner sc = new Scanner(System.in);
int list1Size = sc.nextInt();
int xMin = XMAX + 1;
int[] list = new int[RANGE_SIZE];
int[] list1 = new int[list1Size];
for (int i = 0; i < list1Size; i++) {
list1[i] = sc.nextInt();
}
int list2Size = sc.nextInt();
int[] list2 = new int[list2Size];
for (int i = 0; i < list2Size; i++) {
int num = sc.nextInt();
if (num < xMin) {
xMin = num;
}
list2[i] = num;
}
for (int i = 0; i < list2Size; i++) {
list[list2[i] - xMin]++;
}
for (int i = 0; i < list1Size; i++) {
list[list1[i] - xMin]--;
}
for (int i = 0; i < RANGE_SIZE; i++) {
if (list[i] > 0) {
System.out.print((i + xMin) + " ");
}
}
}
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.
https://sisijava.wordpress.com/2014/07/23/hackerrank-missing-numbers/
Using hashmap.
Actually we just need one hashmap.
http://aruncyberspace.blogspot.com/2014/04/algorithms-search-missing-numbers.html
// No point to sort a,b first
https://codepair.hackerrank.com/paper/keonZBPs?b=eyJyb2xlIjoiY2FuZGlkYXRlIiwibmFtZSI6ImplZmZlcnl5dWFuIiwiZW1haWwiOiJ5dWFueXVuLmtlbm55QGdtYWlsLmNvbSJ9
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
final int XMAX = 10000;
final int RANGE_SIZE = 101;
Scanner sc = new Scanner(System.in);
int list1Size = sc.nextInt();
int xMin = XMAX + 1;
int[] list = new int[RANGE_SIZE];
int[] list1 = new int[list1Size];
for (int i = 0; i < list1Size; i++) {
list1[i] = sc.nextInt();
}
int list2Size = sc.nextInt();
int[] list2 = new int[list2Size];
for (int i = 0; i < list2Size; i++) {
int num = sc.nextInt();
if (num < xMin) {
xMin = num;
}
list2[i] = num;
}
for (int i = 0; i < list2Size; i++) {
list[list2[i] - xMin]++;
}
for (int i = 0; i < list1Size; i++) {
list[list1[i] - xMin]--;
}
for (int i = 0; i < RANGE_SIZE; i++) {
if (list[i] > 0) {
System.out.print((i + xMin) + " ");
}
}
}