PythonMediumTwo Pointers20 min

Merge two sorted arrays

Keep every occurrence of duplicate values. Either input array may be empty. Interviewers use this problem to assess advance a pointer only in the array supplying the next output.

Input

First line: n and m. Second line: n sorted integers. Third line: m sorted integers.

Output

Print the result values separated by single spaces; print an empty line for an empty result.

Constraints

0 <= n, m <= 100000; -10^9 <= values <= 10^9.

Example

Input

3 3
1 3 5
2 4 6

Output

1 2 3 4 5 6

Example 2 input

2 2
1 1
1 1

Output

1 1 1 1

Duplicate values from both arrays must all be retained.

Public test cases (2)

  • Input

    3 3
    1 3 5
    2 4 6

    Expected output

    1 2 3 4 5 6
  • Input

    3 2
    -3 0 0
    0 9

    Expected output

    -3 0 0 0 9
Hints (2)
  1. Identify the state needed to avoid repeated work.
  2. Advance a pointer only in the array supplying the next output.

Target complexity: time O(n + m) · space O(n + m) for the result

Your solution

Checking runner availability…

Reference solution

Sign in for solution
All Python programs