LeetCode 88: Merge Sorted Arrays

Pierre-Marie Poitevin
3 min readApr 22, 2024

In this exercise, we need to take 2 sorted arrays and merge them into one sorted array. The change should be done “in place” and the result stored in the first array.

a. Basics of merging arrays

When 2 arrays nums1 and nums2, are sorted, we merge them by traversing both arrays simultaneously, keeping a pointer i for the next element in nums1 and j for the next element in nums2. At each step, we compare nums1[i] and nums2[j], we add the lowest to our current result and increment the index for the corresponding array. If nums1 is of…

--

--