Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/ikki-korsatkich-kesishma

Intersection of sorted arrays

Easy 1000 ms 128 MB 22%
Share

Two integer arrays sorted in non-decreasing order are given: $a$ with $n$ elements and $b$ with $m$ elements. Find the numbers that occur in both arrays: if a number occurs $p$ times in $a$ and $q$ times in $b$, it appears in the answer $\min(p, q)$ times. Print the answer in non-decreasing order.

Cells of two sorted arrays: common numbers are joined in pairs, the common part is highlighted

Input

The first line contains a positive integer $n$, the second line contains the $n$ integers of array $a$. The third line contains a positive integer $m$, the fourth line contains the $m$ integers of array $b$.

Output

The first line contains the number of common elements $k$. If $k \gt 0$, the second line contains $k$ integers separated by spaces.

Note

$1 \le n, m \le 10^{5}$
$-10^{9} \le a_{i}, b_{j} \le 10^{9}$

Examples

Input #1
6
1 2 2 3 5 8
5
2 2 2 5 9
Output #1
3
2 2 5
Input #2
2
1 3
2
2 4
Output #2
0

Sign in to submit a solution

Signing up is free and takes a minute.