-
Notifications
You must be signed in to change notification settings - Fork 0
/
20_union-of-two-sorted-arrays.java
74 lines (60 loc) · 2.07 KB
/
20_union-of-two-sorted-arrays.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
// 04-2024(April)/20_union-of-two-sorted-arrays.java
/**
* Date : 20-Apr-24
* Repo : https://github.com/ankitsamaddar/daily-geeksforgeeks
*
* Problem : Union of Two Sorted Arrays
* Difficulty: 🟡Medium
*
* GeeksforGeeks : https://www.geeksforgeeks.org/problems/union-of-two-sorted-arrays-1587115621/1
*/
import java.io.*;
import java.util.*;
// User function Template for Java
// arr1,arr2 : the arrays
// n, m: size of arrays
class Solution {
// Function to return a list containing the union of the two arrays.
public static List<Integer> findUnion(int[] arr1, int[] arr2, int n, int m) {
// TreeSet sorts in ascending order
Set<Integer> union = new TreeSet<>();
// Add all elements of the first array to the union.
for (int i = 0; i < n; i++) {
union.add(arr1[i]);
}
// Add all elements of the second array to the union.
for (int j = 0; j < m; j++) {
union.add(arr2[j]);
}
// Convert the LinkedHashSet to an ArrayList and return it.
return new ArrayList<>(union);
}
}
//{ Driver Code Starts
// Initial Template for Java
class Main {
public static void main(String args[]) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {
String st[] = read.readLine().trim().split(" ");
int N = Integer.parseInt(st[0]);
int M = Integer.parseInt(st[1]);
int arr1[] = new int[N];
int arr2[] = new int[M];
st = read.readLine().trim().split(" ");
for (int i = 0; i < N; i++)
arr1[i] = Integer.parseInt(st[i]);
st = read.readLine().trim().split(" ");
for (int i = 0; i < M; i++)
arr2[i] = Integer.parseInt(st[i]);
Solution obj = new Solution();
ArrayList<Integer> res = new ArrayList<Integer>();
res = obj.findUnion(arr1, arr2, N, M);
for (int i = 0; i < res.size(); i++)
System.out.print(res.get(i) + " ");
System.out.println();
}
}
}
// } Driver Code Ends