#K70077. Merge Sorted Arrays

    ID: 33228 Type: Default 1000ms 256MiB

Merge Sorted Arrays

Merge Sorted Arrays

You are given two arrays nums1 and nums2, along with two integers m and n. The array nums1 has a size of m+n, where the first m elements denote valid sorted numbers and the last n elements are set to 0 as placeholders. Your task is to merge nums2 into nums1 so that the final array is sorted in non-decreasing order.

You must perform the merge in-place without using extra storage for another array. The merging strategy should ideally utilize a two-pointer approach starting from the end of each array.

inputFormat

The input consists of three lines:

  • The first line contains two integers, m and n, representing the number of valid elements in nums1 and the number of elements in nums2, respectively.
  • The second line contains m+n integers, representing the contents of nums1 (the last n elements are placeholders and initially 0).
  • If n > 0, the third line contains n integers, representing the elements of nums2.

outputFormat

Output a single line containing m+n integers denoting the merged array in non-decreasing order, separated by spaces.

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