Merge Into the Zero-Padded Array
Problem statement
You are given two integer arrays a and b, both sorted from smallest to largest. Let m = b.length. The first a.length - m values of a are its real values; the last m places of a hold 0 and are only room for the values of b.
Merge b into a, so that a holds all real values of both arrays in sorted order, and return a. Try to do it in place, using only a few extra variables. A 0 among the real values (or in b) is a real value like any other.
Function
mergeIntoPadded(a: int[], b: int[]) → int[]Examples
Example 1
a = [1,4,7,0,0]b = [2,9]return = [1,2,4,7,9]The first three values of a are real; the two zeros at the end are room for b. Merging 1, 4, 7 with 2, 9 gives 1, 2, 4, 7, 9.
Example 2
a = [0,0,0]b = [-5,0,3]return = [-5,0,3]a has no real values, only room for 3. The answer is b itself. The 0 in b is a real value.
Example 3
a = [-3,0,6]b = []return = [-3,0,6]b is empty, so nothing at the end of a is room: here the 0 is a real value and a stays as it is.
Constraints
1 <= a.length <= 3000and0 <= b.length <= a.length-10^5 <= a[i], b[i] <= 10^5- With
m = b.length, the firsta.length - mvalues ofaare sorted, its lastmvalues are all0, andbis sorted.