#K42377. Merge Two Sorted Lists
Merge Two Sorted Lists
Merge Two Sorted Lists
You are given two sorted arrays \(a\) and \(b\) each containing \(n\) integers in non-decreasing order. Your task is to merge these two arrays into a single sorted array in non-decreasing order.
Input Format: The first line of input contains an integer \(T\) denoting the number of test cases. Each test case consists of three lines. The first line contains an integer \(n\), the number of elements in each array. The second line contains \(n\) space-separated integers representing array \(a\). The third line contains \(n\) space-separated integers representing array \(b\).
Output Format: For each test case, output the merged sorted array, with the elements separated by spaces. Each merged array should be printed on a new line.
Example:
Input: 3 3 1 3 5 2 4 6 4 1 2 2 2 1 1 2 2 5 1 2 3 4 5 5 6 7 8 9</p>Output: 1 2 3 4 5 6 1 1 1 2 2 2 2 2 1 2 3 4 5 5 6 7 8 9
Note: It is guaranteed that the input arrays are already sorted. You should aim to use a two-pointer technique to merge the arrays in \(O(n)\) time per test case.
inputFormat
The input is provided via stdin
and has the following format:
- The first line contains a single integer \(T\) denoting the number of test cases.
- For each test case, the first line contains a single integer \(n\) (the size of each array).
- The second line contains \(n\) space-separated integers (elements of the first sorted array \(a\)).
- The third line contains \(n\) space-separated integers (elements of the second sorted array \(b\)).
outputFormat
For each test case, output one line containing the merged sorted array. The numbers should be separated by a single space.
## sample3
3
1 3 5
2 4 6
4
1 2 2 2
1 1 2 2
5
1 2 3 4 5
5 6 7 8 9
1 2 3 4 5 6
1 1 1 2 2 2 2 2
1 2 3 4 5 5 6 7 8 9
</p>