-
Notifications
You must be signed in to change notification settings - Fork 0
/
19_find-missing-in-second-array.java
81 lines (70 loc) · 2.23 KB
/
19_find-missing-in-second-array.java
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// 04-2024(April)/19_find-missing-in-second-array.java
/**
* Date : 19-Apr-24
* Repo : https://github.com/ankitsamaddar/daily-geeksforgeeks
*
* Problem : Find missing in second array
* Difficulty: 🟢Easy
*
* GeeksforGeeks : https://www.geeksforgeeks.org/problems/in-first-but-second5423/1
*/
import java.io.*;
import java.util.*;
// User function Template for Java
class Solution {
ArrayList<Integer> findMissing(int a[], int b[], int n, int m) {
HashSet<Integer> set = new HashSet<>();
// Hash elements of the second array to the set
for (int i = 0; i < m; i++) {
set.add(b[i]);
}
ArrayList<Integer> missingElements = new ArrayList<>();
// Iterate the first array and store missing elements in second array
for (int i = 0; i < n; i++) {
if (!set.contains(a[i])) {
missingElements.add(a[i]);
}
}
return missingElements;
}
}
//{ Driver Code Starts.
//{ Driver Code Starts
// Initial Template for Java
// Driver class
class Array {
// Driver code
public static void main(String[] args) throws IOException {
// Taking input using buffered reader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int testcases = Integer.parseInt(br.readLine());
// looping through all testcases
while (testcases-- > 0) {
// int n = Integer.parseInt(br.readLine());
String line = br.readLine();
String[] q = line.trim().split("\\s+");
int n = Integer.parseInt(q[0]);
int m = Integer.parseInt(q[1]);
// int y =Integer.parseInt(q[2]);
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();
}
}
}
// } Driver Code Ends