easyTwo PointersPure DSA~13 min
Merge Sorted Leaderboards
Two regional leaderboards are each already sorted by score ascending. A global view needs them merged into one sorted list without re-sorting from scratch.
Problem
Given two arrays a and b, each sorted in non-decreasing order, return a single non-decreasing array containing all their elements.
Input
Arrays a (length m) and b (length n), 0 ≤ m, n ≤ 10^5, values in [-10^9, 10^9].
Output
A merged array of length m + n in non-decreasing order.
Constraints
- Both inputs are already sorted ascending
- 0 ≤ m, n ≤ 100,000
- Do it in O(m + n) without a full re-sort
Examples
Example 1
Input
a = [1, 4, 7], b = [2, 3, 9]
Output
[1, 2, 3, 4, 7, 9]
Two pointers advance through a and b, always emitting the smaller front element.
Example 2
Input
a = [], b = [5]
Output
[5]
One side empty — append the remainder of the other.