#K70077. Merge Sorted Arrays
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
andn
, representing the number of valid elements innums1
and the number of elements innums2
, respectively. - The second line contains
m+n
integers, representing the contents ofnums1
(the lastn
elements are placeholders and initially 0). - If
n > 0
, the third line containsn
integers, representing the elements ofnums2
.
outputFormat
Output a single line containing m+n
integers denoting the merged array in non-decreasing order, separated by spaces.
3 3
1 2 3 0 0 0
2 5 6
1 2 2 3 5 6