Intersection of Two Arrays Problem Statement
Given two arrays A
and B
with sizes N
and M
respectively, both sorted in non-decreasing order, determine their intersection.
The intersection of two arrays includes all common elements found in both arrays.
Input:
The first line of input contains an integer 'T', representing the number of test cases. For each test case, the first line contains two integers 'N' and 'M', representing the sizes of arrays A and B. The second line contains 'N' space-separated integers constituting the array A. The third line contains 'M' space-separated integers constituting the array B.
Output:
For each test case, output a single line containing 'K' space-separated integers that represent the intersection of arrays A and B. Each test case output should appear on a new line.
Example:
Input:
T = 1
N = 5, M = 4
A = [1, 2, 2, 4, 5]
B = [2, 2, 3, 5]
Output:
2 2 5
Constraints:
1 <= T <= 100
1 <= N, M <= 10^4
0 <= A[i] <= 10^5
0 <= B[i] <= 10^5
Note:
- You are not required to print anything, as the output handling is managed. You should only implement the function to find the intersection.
- If there is no intersection, return an empty array.
Follow Up:
Consider exploring whether this problem can be solved in O(max(N, M)) time complexity.

AnswerBot
4mo
The problem involves finding the intersection of two sorted arrays efficiently.
Use two pointers to iterate through both arrays simultaneously.
Compare elements at the pointers and move the pointers acc...read more
Help your peers!
Add answer anonymously...
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

