Union and Intersection of Two Arrays
Given two arrays 'A' and 'B' of size 'N' and 'M' respectively, both sorted in non-decreasing order, find the intersection of these two arrays.
The intersection of two arrays consists of all common elements occurring in both arrays.
Input:
The first line of input contains an integer 'T', denoting the number of test cases.
The first line of each test case contains two integers 'N' and 'M', denoting the sizes of the two arrays.
The second line of each test case contains 'N' space-separated integers denoting elements of the first array.
The third line of each test case contains 'M' space-separated integers denoting elements of the second array.
Output:
For each test case, output a single line containing 'K' space-separated integers which correspond to the intersection of arrays A and B.
Example:
Input:
2
4 5
1 2 4 5
2 3 4 5 6
3 3
7 7 8
5 7 8
Output:
2 4 5
7 8
Constraints:
- 1 <= T <= 100
- 1 <= N, M <= 104
- 0 <= A[i], B[i] <= 105
- Time Limit: 1 sec
Note:
You do not need to print anything; implement the given function as the output handling is managed.
Follow-Up:
Can we solve this problem using a time complexity of O(max(N, M))?
Be the first one to answer
Add answer anonymously...
Popular interview questions of Mts1
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app