#K86622. Merge Sorted Arrays
Merge Sorted Arrays
Merge Sorted Arrays
You are given two sorted arrays. Your task is to merge them into a single sorted array without using any built-in sorting functions.
The merging process must be performed in linear time by using a two-pointer technique.
Example: For input arrays [1, 3, 5] and [2, 4, 6], the output should be [1, 2, 3, 4, 5, 6].
In mathematical terms, if you have two sequences \(A = [a_1, a_2, \dots, a_n]\) and \(B = [b_1, b_2, \dots, b_m]\) with \(a_i \leq a_{i+1}\) and \(b_j \leq b_{j+1}\), then the merged sequence \(C\) must satisfy:
\[ C = A \cup B \quad \text{and} \quad \forall k, \; c_k \leq c_{k+1} \]You should implement an efficient merging strategy that reads input from stdin and writes the result to stdout.
inputFormat
The input is given in the following format from standard input:
$n$ $a_1$ $a_2$ ... $a_n$ $m$ $b_1$ $b_2$ ... $b_m$
Where:
- $n$ is the number of elements in the first array. If $n = 0$, the next line will be empty.
- The second line contains $n$ integers (the first sorted array).
- $m$ is the number of elements in the second array. If $m = 0$, the next line will be empty.
- The fourth line contains $m$ integers (the second sorted array).
outputFormat
Output the merged sorted array as a sequence of integers separated by a single space. If the merged array is empty, output nothing.
## sample3
1 3 5
3
2 4 6
1 2 3 4 5 6