Back to DSA

Merge Sorted Array

easy
Acceptance: 55%
ArraysTwo Pointers

Two sorted integer arrays are provided along with their respective element counts. The first array has extra space at the end to accommodate all elements from the second. Combine both arrays into a single sorted sequence, storing the result directly in the first array without allocating additional storage.

Examples

Example 1:
Input:a = [2,4,7,0,0,0], m = 3, b = [1,3,6], n = 3
Output:[1,2,3,4,6,7]
Explanation: Merging [2,4,7] with [1,3,6] produces the sorted result [1,2,3,4,6,7].
Example 2:
Input:a = [5], m = 1, b = [], n = 0
Output:[5]
Explanation: The second array is empty, so the first array stays unchanged.

Hints

00:00
1234567