DSA · Two pointers (in-place) · #27
Merge Sorted Array
Module 30 · difficulty 2/5·⏱ 30:00starts on first keystroke
You are given two sorted ascending arrays `nums1` and `nums2`, and integers `m` and `n` (the number of real elements in each). `nums1` has length `m + n`, with the last `n` slots set to 0 as padding. Merge `nums2` into `nums1` in-place so `nums1` becomes one sorted array. (Return value is ignored.)
Examples
nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3→[1,2,2,3,5,6]nums1 = [1], m = 1, nums2 = [], n = 0→[1]nums1 = [0], m = 0, nums2 = [1], n = 1→[1]
Stuck? Reveal an animated walkthrough of the approach.
Constraints
- · nums1.length == m + n
- · nums2.length == n
- · 0 <= m, n <= 200
- · -10^9 <= nums1[i], nums2[i] <= 10^9
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.