← All problems
Merge Sorted Array
You are given two sorted integer arrays nums1 (with m elements followed by n zero slots) and nums2 (with n elements). Merge nums2 into nums1 in place so that nums1 becomes a single sorted array of length m + n. You must do this with O(1) extra space — merging from the front would overwrite unprocessed elements.
Complexity
Time O(m + n), space O(1).
Common pitfalls
- Merging front-to-back and clobbering nums1's unprocessed tail.
- Forgetting to copy remaining nums2 elements when nums1 is exhausted first.
- Off-by-one errors in the initial pointer positions.
Your solution
Log in to save your code and track progress.