Two Pointers pattern

Merge Sorted Array

nums1 holds m sorted values followed by n zeros of padding; nums2 holds n sorted values. Merge nums2 into nums1 in place so nums1 ends up sorted. Watch it run step by step, then write your own pseudocode and get it checked by AI.

  • LeetCode 88
  • Easy
  • 2 approaches

Sign in with Google or GitHub and you land straight on this page. DSA comes with the all labs plan and every ChaiCode batch.

Two PointersLeetCode 88

Easy

From the first idea to the best one

  1. 01 Brute force · concatenate + sort O((m+n) log(m+n))O(1) space
  2. 02 Optimized · merge backwards O(m + n)O(1) space
Each approach plays step by step, with its time and space.

What you will learn

  • How to read Merge Sorted Array and spot the two pointers pattern in it
  • Brute force · concatenate + sort: O((m+n) log(m+n)) time, O(1) space
  • Optimized · merge backwards: O(m + n) time, O(1) space
  • The pseudocode for each approach, its line lit as the animation runs
  • Practice: write your own pseudocode and get it checked

More Two Pointers

See Merge Sorted Array run, step by step

Sign in and this page opens on the animation, with all 12 Two Pointers problems beside it.

DSA comes with the all labs plan and every ChaiCode batch.