Two arrays of integers a and b are given, both sorted in
non-decreasing order (they may have repeated values within each array). Print
the values that appear in both arrays, each common value
printed exactly once, in non-decreasing order.
The required algorithm is a variant of the two-pointer merge: when
a[i] < b[j] advance i, when a[i] > b[j] advance j, and on
equality add the value to the result and advance both indices.
Input:
The program reads from standard input:
- a natural number n, followed by n integers (array a);
- a natural number m, followed by m integers (array b).
- 1 <= n, m <= 1000
- -1000000 <= values <= 1000000
- the arrays are sorted in non-decreasing order and may contain duplicates
Output:
The program prints on the first line the number k of distinct values common
to both arrays. If k > 0, the second line contains the k
values, separated by a single space. If k = 0, no further line is printed.
Example:
Input:
5
1 2 3 4 5
4
2 4 6 8
Output:
2
2 4