Skip to content
Problem 88 · LeetCode 88(opens in a new tab)

Merge Sorted Array

EasyArrays

You get two integer arrays, nums1 and nums2, each sorted from small to large, plus two counts: m and n. Only the first m slots of nums1 hold real values. Its last n slots are filler zeros, there to make room. nums2 holds exactly n values.

Combine the two into one sorted sequence, written into `nums1` itself. Return nothing: the caller reads nums1 after your function ends.

Example 1

Input:  nums1 = [2, 4, 7, 0, 0, 0], m = 3, nums2 = [1, 5, 8], n = 3
Output: [1, 2, 4, 5, 7, 8]
Why:    the real values 2, 4, 7 and 1, 5, 8 interleave
2 more examples and the constraints

Example 2

Input:  nums1 = [5, 6, 0, 0], m = 2, nums2 = [1, 2], n = 2
Output: [1, 2, 5, 6]
Why:    every value of nums2 is smaller, so it all goes first

Example 3

Input:  nums1 = [0], m = 0, nums2 = [4], n = 1
Output: [4]
Why:    m = 0, so the 0 is only room, not a value

Constraints

  • nums1 has length m + n, and nums2 has length n
  • 0 ≤ m, n ≤ 200, and 1 ≤ m + n ≤ 200
  • −1,000,000,000 ≤ each value ≤ 1,000,000,000
  • Both inputs are already sorted in non-decreasing order

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Copy, then sortO(k log k)
  2. Merge from the front, with a copyO(m + n)
  3. Merge from the backO(m + n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for m + n = 200Verdict
Copy, then sortO(k log k)
Simple; ignores that the inputs are sorted
Merge from the frontO(m + n)
Linear, but needs a copy of nums1
Merge from the backO(m + n)
Best; linear and in place

m and n = number of real values in nums1 and nums2, k = m + n.

Edge cases to test

  • nums2 is empty. n = 0: nums1 is already the answer, and the loop never runs.
  • nums1 has no real values. m = 0: i starts at −1, so every write comes from nums2.
  • Everything in nums2 is smaller. All of nums1's values shift to the back first, then nums2 fills the front.
  • Equal values. Ties can go either way; the result is the same sorted list.
  • Real zeros. A 0 inside the first m slots is a value, not filler. Trust m, not the zeros.

Common mistakes

  • Looping while i >= 0 and j >= 0 and forgetting to copy what is left of nums2 afterwards.
  • Reading nums1[i] when i is already −1, which wraps to the last element in Python.
  • Merging from the front without a copy, so values of nums1 are overwritten before they are read.

How to talk through it

  1. 1Clarify. Is the filler always at the end of nums1? Can m or n be 0? Do I return anything?
  2. 2Start simple. Copy nums2 into the tail and sort: correct, but O(k log k).
  3. 3Use the order. Both inputs are sorted, so a two-pointer merge is O(m + n). From the front it needs a copy.
  4. 4Flip the direction. Fill from the back: the free slots are there, so no copy is needed and nothing is overwritten early.
  5. 5Test out loud. Try n = 0, m = 0, and a case where all of nums2 is smaller.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?