#C7140. Merge Sorted Arrays

    ID: 50979 Type: Default 1000ms 256MiB

Merge Sorted Arrays

Merge Sorted Arrays

You are given two sorted integer arrays. The first array, \(\textbf{nums1}\), has a size of \(m+n\) with its first \(m\) elements being sorted and the remaining \(n\) slots filled with zeros (as placeholders). The second array, \(\textbf{nums2}\), contains \(n\) sorted integers. Your task is to merge \(\textbf{nums2}\) into \(\textbf{nums1}\) in such a way that the final array is also sorted in non-decreasing order.

Note: You must perform the merge in-place and use the concept of two pointers starting from the end of the arrays.

For example, given \(\textbf{nums1} = [1,2,3,0,0,0]\) with \(m=3\) and \(\textbf{nums2} = [2,5,6]\) with \(n=3\), the merged array should be \( [1,2,2,3,5,6] \).

inputFormat

The input is read from stdin and follows this format:

  • The first line contains an integer \(T\) representing the number of test cases.
  • For each test case:
    • The first line contains two integers \(m\) and \(n\) separated by a space.
    • The second line contains \(m\) space-separated integers representing the first sorted array.
    • If \(n > 0\), the third line contains \(n\) space-separated integers representing the second sorted array.

For the purpose of merging, the first array should be treated as having a total length of \(m+n\), where the last \(n\) positions are initially filled with zeros.

outputFormat

For each test case, output the merged sorted array as a sequence of space-separated integers on one line to stdout.

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