#K5481. Merge Two Sorted Arrays

    ID: 29836 Type: Default 1000ms 256MiB

Merge Two Sorted Arrays

Merge Two Sorted Arrays

You are given two sorted arrays. Your task is to merge them into a single sorted array. The merged array should preserve the order, and all elements from both arrays must appear, including duplicates.

Note: The arrays might be empty. You should read the input from standard input (stdin) and output the result to standard output (stdout). Use efficient merging techniques with two pointers.

The problem can be mathematically described as follows:

If \(A = [a_1, a_2, \ldots, a_n]\) and \(B = [b_1, b_2, \ldots, b_m]\) are two sorted arrays, then produce an array \(C\) such that \(C\) contains all elements of \(A\) and \(B\) in non-decreasing order.

inputFormat

The input is given in the following format:

  • The first line contains an integer \(n\), representing the number of elements in the first array.
  • The second line contains \(n\) space-separated integers representing the first sorted array.
  • The third line contains an integer \(m\), representing the number of elements in the second array.
  • The fourth line contains \(m\) space-separated integers representing the second sorted array.

You must read from standard input.

outputFormat

Output the merged sorted array as a single line of space-separated integers. There should be no extra spaces at the beginning or at the end of the line.

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