All problems

Problem 702

Union of Two Sorted Arrays

Easy · 10 pts · Array, Two Pointers

Problem

You are given two arrays, each sorted in non-decreasing order, and either may contain repeated values. Print their union: every distinct value that appears in at least one of the arrays, once each, in increasing order. Walk through both arrays with two pointers like the merge step of merge sort, always taking the smaller current value and skipping it if it equals the last value written.

Input

The first line contains N and M. The second line contains the N values of the first array. The third line contains the M values of the second array.

Output

Print the distinct values of the union in increasing order, separated by spaces.

Constraints

1 <= N, M <= 100000; -10^9 <= values <= 10^9; both arrays are sorted

Example input

6 6
1 1 2 3 4 5
2 3 4 4 5 6

Example output

1 2 3 4 5 6

Explanation

For 1 1 2 3 4 5 and 2 3 4 4 5 6, the distinct values that appear in either array are 1, 2, 3, 4, 5 and 6, so the union is 1 2 3 4 5 6.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Check if Array Is Sorted and Rotated Next: Count Subarrays with XOR K