15% off all plans & roadmaps Use code
See plansLeetCode 88 · Solution & editorial
You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.
Merge Sorted Array is a Array/Matrix Manipulation Patterns problem, in the Merge Sorted Array (In-place from End) subpattern. The pattern covers in-place operations, hashing, prefix sums, spiral traversal, and cyclic sort
Learn the merge sorted array technique starting from the end. Essential for space-efficient array merging in coding interviews.
The reference solution runs in O(m + n) time and O(1) space.
Merge Sorted Array has been reported in interviews at Facebook, Microsoft, Amazon, Bloomberg, Walmart Labs, IBM, Oracle, Apple.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Merge Sorted Array is a Array/Matrix Manipulation Patterns problem, in the Merge Sorted Array (In-place from End) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.