-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersectionOfArrays.java
More file actions
84 lines (77 loc) · 3.05 KB
/
Copy pathIntersectionOfArrays.java
File metadata and controls
84 lines (77 loc) · 3.05 KB
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
82
83
84
/*Qus 3:-Intersection of Two Arrays II
You have been given two integer arrays/list(ARR1 and ARR2) of size N and M, respectively. You need to print their intersection; An intersection for this
problem can be defined when both the arrays/lists contain a particular value or to put it in other words, when there is a common value that exists in both the arrays/lists.
Note :
Input arrays/lists can contain duplicate elements.
The intersection elements printed would be in the order they appear in the first array/list(ARR1)
Input format :
The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
First line of each test case or query contains an integer 'N' representing the size of the first array/list.
Second line contains 'N' single space separated integers representing the elements of the first the array/list.
Third line contains an integer 'M' representing the size of the second array/list.
Fourth line contains 'M' single space separated integers representing the elements of the second array/list.
Output format :
For each test case, print the intersection elements in a row, separated by a single space.
Output for every test case will be printed in a separate line. Constraints :
1 <= t <= 10^2
0 <= N <= 10^3
0 <= M <= 10^3
Time Limit: 1 sec Sample Input 1 :
2
6
2 6 8 5 4 3
4
2 3 4 7
2
10 10
1
10
Sample Output 1 :
2 4 3
10
Sample Input 2 :
1
4
2 6 1 2
5
1 2 3 4 2
Sample Output 2 :
2 1 2
Explanation for Sample Output 2 :
Since, both input arrays have two '2's, the intersection of the arrays also have two '2's. The first '2' of first array matches with the first '2' of the second array.*/
import java.util.Scanner;
class Intersection {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt(); // Number of test cases
for (int i = 0; i < tc; i++) {
int size1 = sc.nextInt();
int[] nums1 = new int[size1];
// Read elements of nums1
for (int l = 0; l < size1; l++) {
nums1[l] = sc.nextInt();
}
int size2 = sc.nextInt();
int[] nums2 = new int[size2];
// Read elements of nums2
for (int m = 0; m < size2; m++) {
nums2[m] = sc.nextInt();
}
// Find and print intersection elements
boolean isFirstPrinted = false;
for (int n = 0; n < size1; n++) {
for (int j = 0; j < size2; j++) {
if (nums1[n] == nums2[j]) {
if (isFirstPrinted) {
System.out.print(" ");
}
System.out.print(nums1[n]);
isFirstPrinted = true;
break; // Break inner loop after finding a match
}
}
}
System.out.println(); // Print newline after each test case's intersection elements
}
}
}