You are given two integer arrays. Compute their multiset intersection: a value that appears x times in the first array and y times in the second array appears min(x, y) times in the result. Print all resulting values in ascending order (repeats included).
If the intersection is empty, print an empty line.
Line 1: an integer n, the size of the first array.
Line 2: n space-separated integers, the first array.
Line 3: an integer m, the size of the second array.
Line 4: m space-separated integers, the second array.
One line: the multiset intersection in ascending order, space-separated. If empty, print an empty line.
Example 1
Input
4 1 2 2 3 3 2 2 4
Expected
2 2
Explanation
Value 2 appears twice in the first array and twice in the second, so min is 2. No other value is shared, giving '2 2'.
Example 2
Input
3 5 6 7 2 8 9
Expected
(empty)Explanation
No value appears in both arrays, so the output is an empty line.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →