Sorted Arrays Intersection

Easy

Given 2 sorted arrays, return the intersection of both the arrays. The intersection of 2 arrays means all the elements which are present in both.

Example
Array 1: [1, 3, 4, 5, 5, 6, 6, 7]
Array 2: [2, 5, 6, 6, 7, 8]
Intersection: [5, 6, 6, 7]

Note: The resultant array should also be sorted.

Testing

Input Format

The first line contains an integer ‘T’ denoting the number of independent test cases.

For each test case, the input has three lines:

  • The first line contains two integers ‘n’ and ‘m’ denoting the length of the arrays A and B respectively.
  • The second line contains n space-separated integers denoting the elements of A.
  • The third line contains m space-separated integers denoting the elements of B.

Output Format

For each test case, print space-separated numbers denoting the intersection of the two sorted arrays.

Sample Input

3
4 4
1 2 3 4
1 3 4 5
4 5
1 1 3 3
3 3 4 5 6
8 6
1 3 4 5 5 6 6 7
2 5 6 6 7 8

Expected Output

1 3 4
3 3
5 6 6 7

Constraints

1 <= T <= 100

1 <= n, m <= 104

1 <= Ai, Bi <= 105

Editorial Link: Editorial