Merge Two Sorted Arrays
L3 Easy Two Pointers
Merging two sorted arrays can be done in one backward pass: fill the target array from its end so nothing is overwritten prematurely.
You are given nums1 (length m + n where the last n slots are zero placeholders) and nums2 (length n), each sorted in non-decreasing order. Merge nums2 into nums1 in place so the whole array ends up sorted. The first m slots of nums1 hold the real values. Return nothing.
Examples
▸ nums1 = [1, 2, 3, 0, 0, 0], m = 3, nums2 = [2, 5, 6], n = 3
→ [1, 2, 2, 3, 5, 6]
▸ nums1 = [1], m = 1, nums2 = [], n = 0
→ [1]
▸ nums1 = [0], m = 0, nums2 = [1], n = 1
→ [1]