Merge Sorted Array: Optimal O(m+n) Solution (Data-Backed Guide)

By DSA Prep Team · March 4, 2026 · 7 min read · Algorithms

Merge Sorted Array is the #14 most asked LeetCode problem globally — the classic in-place merge problem that tests whether you can think backwards.

Our analysis of 10,385 verified interview questions across 259 companies shows it appeared 65 times across 26 companies — including Google, Amazon, Meta, Microsoft, Goldman Sachs, Bloomberg, LinkedIn, ByteDance, and Expedia.

The naive approach copies elements and wastes O(m+n) space. The elegant solution merges from the end — placing the largest elements first into the buffer space already available at the tail of nums1. O(m+n) time, O(1) space.

Table of Contents

The Data: 26 Companies Ask This

MetricValue
Total Interview Appearances65 verified mentions
Companies That Ask It26 companies
Global Rank#14 out of 10,385 questions
DifficultyEasy
Core PatternsArray, Two Pointers, Sorting

📌 Companies That Ask This Problem

Google, Amazon, Meta, Microsoft, Goldman Sachs, Bloomberg, LinkedIn, ByteDance, Expedia, Adobe, Agoda, Apple, Atlassian, Avito, Barclays, Cisco, Cognizant, Criteo, EPAM Systems, HCL, Hubspot, IBM, Infosys, Intel, and 2 more.

Problem Statement

LeetCode #88 — Merge Sorted Array

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. nums1 has a length of m + n, where the last n elements are set to 0. Merge nums2 into nums1 in-place sorted order.

Example
Input:  nums1 = [1,2,3,0,0,0], m=3, nums2 = [2,5,6], n=3
Output: nums1 = [1,2,2,3,5,6]

Input:  nums1 = [1], m=1, nums2 = [], n=0
Output: nums1 = [1]

Input:  nums1 = [0], m=0, nums2 = [1], n=1
Output: nums1 = [1]

The Core Intuition: Merge from the End

Merging from the front causes overwrites — nums1[0] gets clobbered before you use it. Merging from the back avoids this entirely: the tail of nums1 (positions m to m+n-1) is all zeros — empty buffer space.

Use three pointers: p1 = m-1 (last real element in nums1), p2 = n-1 (last element in nums2), p = m+n-1 (write position). At each step, place the larger of nums1[p1] and nums2[p2] at position p and move the corresponding pointer left.

💡 Why This Works

We're writing from right to left into the buffer. We can never overwrite an element we still need, because the write pointer p always stays ahead of (or at) the read pointer p1.

Code in Python, Java & JavaScript

Python 3
def merge(nums1: list[int], m: int, nums2: list[int], n: int) -> None:
    p1 = m - 1       # pointer for nums1 real elements
    p2 = n - 1       # pointer for nums2
    p = m + n - 1    # write pointer (end of nums1)

    while p1 >= 0 and p2 >= 0:
        if nums1[p1] > nums2[p2]:
            nums1[p] = nums1[p1]
            p1 -= 1
        else:
            nums1[p] = nums2[p2]
            p2 -= 1
        p -= 1

    # If nums2 has remaining elements, copy them
    # (nums1 remaining elements are already in place)
    while p2 >= 0:
        nums1[p] = nums2[p2]
        p2 -= 1
        p -= 1
Java
class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int p1 = m - 1, p2 = n - 1, p = m + n - 1;

        while (p1 >= 0 && p2 >= 0) {
            if (nums1[p1] > nums2[p2])
                nums1[p--] = nums1[p1--];
            else
                nums1[p--] = nums2[p2--];
        }

        while (p2 >= 0)
            nums1[p--] = nums2[p2--];
    }
}
JavaScript
var merge = function(nums1, m, nums2, n) {
    let p1 = m - 1, p2 = n - 1, p = m + n - 1;

    while (p1 >= 0 && p2 >= 0) {
        if (nums1[p1] > nums2[p2])
            nums1[p--] = nums1[p1--];
        else
            nums1[p--] = nums2[p2--];
    }

    while (p2 >= 0)
        nums1[p--] = nums2[p2--];
};

Complexity Analysis

📊 Complexity

How to Actually Remember This

✅ Spaced Repetition Schedule

  1. Today: Implement from scratch. Trace through [1,2,3,0,0,0] + [2,5,6] step by step.
  2. Day 3: Solve LC #23 (Merge K Sorted Lists) — same merge logic, extended to K arrays using a heap.
  3. Day 7: Re-implement without notes. Focus on why you don't need to copy remaining nums1 elements.

Tag on DSAPrep.dev under Two Pointers and Sorting.

Track on DSAPrep.dev →