#C12864. Merge Sorted Arrays

    ID: 42338 Type: Default 1000ms 256MiB

Merge Sorted Arrays

Merge Sorted Arrays

You are given two sorted arrays. Your task is to merge these two arrays into one sorted array without using any built-in sort functions. The process should be done using the two-pointer technique, ensuring that the final merged array is sorted in non-decreasing order.

Let \(A\) and \(B\) be the two sorted arrays. You are to compute the sorted merge \(C\) such that

[ C = A \cup B ]

for example, if \(A = [1, 3, 5, 7]\) and \(B = [2, 4, 6, 8]\), then the merged array \(C\) should be:

[ C = [1, 2, 3, 4, 5, 6, 7, 8]]

Input is provided via standard input (stdin) and output should be written to standard output (stdout).

inputFormat

The input consists of two lines:

  • The first line contains the first sorted array. The numbers are separated by spaces. If the array is empty, the line will be blank.
  • The second line contains the second sorted array in the same format.

outputFormat

The output is a single line containing the merged sorted array. The numbers should be separated by a single space. If the merged array is empty, output a blank line.

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