#K88732. Merge Sorted Arrays

    ID: 37374 Type: Default 1000ms 256MiB

Merge Sorted Arrays

Merge Sorted Arrays

You are given two sorted arrays, A and B. Array A contains n1 elements and array B contains n2 elements. Your task is to merge these two arrays into one single sorted array in non-decreasing order.

The merging process should be done in O(n1+n2) time complexity. This technique is similar to the merge step in the Merge Sort algorithm. Specifically, you need to implement the merging algorithm using a two-pointer technique.

Note: The arrays provided in the input are already sorted, so you only need to merge them.

The mathematical time complexity can be expressed as:

\(T(n_1,n_2) = O(n_1 + n_2)\)

inputFormat

The input is given via standard input (stdin) and consists of three lines:

  • The first line contains two integers n1 and n2, denoting the sizes of the two arrays.
  • The second line contains n1 space-separated integers representing the first sorted array.
  • The third line contains n2 space-separated integers representing the second sorted array.

You can assume that 0 \(\leq n1, n2 \leq 10^5\) and each array is sorted in non-decreasing order.

outputFormat

Output the merged sorted array to the standard output (stdout) as a single line of space-separated integers.

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