easyArrayTwo PointersTwo Pointers

Merge Sorted Array

easyTime: O(n + m)Space: O(1)

Recognize the pattern

merge two sorted arrays in-placeat end of first arraythree pointers

Brute force idea

The naive version of Merge Sorted Array sounds like this: Copy both into new array and merge. That direct path helps you understand the question, but it tends to treat every possibility as brand new instead of learning from earlier steps.

Better approach

The real unlock in Merge Sorted Array comes when you notice this: Fill from the end using three pointers. Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.

Key invariant

The compass for Merge Sorted Array is this: Fill from the back to avoid overwriting elements in nums1 that haven't been processed. As long as that statement keeps holding, you can trust the steps built on top of it.

Watch out for

The trap in Merge Sorted Array usually looks like this: Filling from the front — you'd overwrite elements in nums1 you still need. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.

Two Pointers Pattern