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
1y
The problem is to find the intersection of two sorted arrays.
Use two pointers to iterate through the arrays.
Compare the elements at the current pointers and move the pointers accordingly.
If the elemen...read more
Help your peers!
Add answer anonymously...
Popular interview questions of Software Developer
A Software Developer was asked Q1. Cycle Detection in a Singly Linked List Determine if a given singly linked list ...read more
A Software Developer was asked Q2. Intersection of Two Arrays Problem Statement Given two arrays A and B with sizes...read more
A Software Developer was asked Q3. Minimum Depth of a Binary Tree Determine the minimum depth of an integer-based b...read more
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

