Easy · Merge Step
Understand → Visualise → Practice.
Merge two ascending arrays into one ascending result while preserving duplicate values.
Both inputs are sorted, so the smallest remaining value is always at the front of one of them. Repeatedly take the smaller front.
i points into a, j into b.
Append a[i] if a[i] ≤ b[j], otherwise b[j], and advance that pointer.
When one list runs out, append the remainder of the other.
O(n + m) time and O(n + m) space.
Using < instead of ≤ still works for values, but ≤ keeps equal values from a first, which makes the merge stable.
Read the full guides first: Sorting Algorithms and Their Trade-offs, Binary Search and Boundary Finding.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.