#C3544. Merge Two Sorted Arrays

    ID: 46983 Type: Default 1000ms 256MiB

Merge Two Sorted Arrays

Merge Two Sorted Arrays

You are given two sorted arrays. Your task is to merge these arrays into a single sorted array. The merged result should contain all the elements from the input arrays, including duplicates, in non-decreasing order.

One efficient approach is to use the two-pointer technique. Specifically, if the two arrays are denoted as \(A\) and \(B\), and their lengths are \(n\) and \(m\) respectively, you should produce a merged array \(C\) as follows:

\[ C = merge(A, B) \quad \text{with runtime } \mathcal{O}(n+m) \]

Handle edge cases such as one or both arrays being empty.

inputFormat

The input consists of two lines:

  • The first line contains the elements of the first sorted array separated by spaces. If it is empty, the array is empty.
  • The second line contains the elements of the second sorted array separated by spaces. Similarly, if it is empty, the array is empty.

outputFormat

Output the merged sorted array as a sequence of integers separated by a space on a single line.

## sample
1 3 5
2 4 6
1 2 3 4 5 6