-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind missing in second array
60 lines (47 loc) · 1.63 KB
/
Find missing in second array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//Find missing in second array
import java.io.*;
import java.util.*;
class Solution {
ArrayList<Integer> findMissing(int a[], int b[], int n, int m) {
ArrayList<Integer> ans = new ArrayList<>();
Map<Integer, Integer> map = new HashMap<>();
for(int elb : b) {
map.put(elb, 1);
}
for(int ela : a) {
if(!map.containsKey(ela)) {
ans.add(ela);
}
}
return ans;
}
}
class Array {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int testcases = Integer.parseInt(br.readLine());
while(testcases-- > 0) {
String line = br.readLine();
String[] q = line.trim().split("\\s+");
int n =Integer.parseInt(q[0]);
int m =Integer.parseInt(q[1]);
String line1 = br.readLine();
String[] a1 = line1.trim().split("\\s+");
int a[] = new int[n];
for(int i = 0; i < n; i++) {
a[i] = Integer.parseInt(a1[i]);
}
String line2 = br.readLine();
String[] a2 = line2.trim().split("\\s+");
int b[] = new int[m];
for(int i = 0; i < m; i++) {
b[i] = Integer.parseInt(a2[i]);
}
Solution ob = new Solution();
ArrayList<Integer> ans=ob.findMissing(a,b,n,m);
for(int i = 0; i < ans.size(); i++) {
System.out.print(ans.get(i)+" ");
}
System.out.println();
}
}