Combine Two Ordered Sequences
Arrays & HashingAuto-graded · 16 test casesLast updated
Tests your ability to merge two pre-sorted arrays into a single sorted result using the classic two-pointer technique.
Combine Two Ordered Sequences
You receive two arrays, each already sorted in ascending order. Your task is to produce a single array containing all elements from both inputs, maintaining sorted order.
Write a function that takes two sorted integer arrays and returns a new sorted array containing every element from both inputs.
Constraints
- Each input array contains between 0 and 50 elements (inclusive)
- All element values are integers in the range [-100, 100]
- Both input arrays are sorted in non-decreasing order
- When both arrays contain equal values, preserve elements from the first array before elements from the second array in the output
Examples
Example 1
Input: combine_sorted_arrays([1, 2, 4], [1, 3, 4])
Output: [1, 1, 2, 3, 4, 4]
Example 2
Input: combine_sorted_arrays([], [0])
Output: [0]
Hints
Hint 1
Since both arrays are already sorted, you only need to compare the current front elements of each array to find the next smallest overall element.
Hint 2
Use two index pointers to track your position in each input array, advancing whichever pointer points to the smaller current element.
Hint 3
Once you exhaust one array, all remaining elements in the other array are already in sorted order and can be appended directly.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem uses the classic two-pointer merge technique, which is the merge step from merge sort. Since both inputs are already sorted, we know that the smallest element overall must be at the front of one of the two arrays.
Maintain one pointer into each array:
- Compare the elements at both current positions
- Take whichever is smaller and add it to the output
- Advance only the pointer from which we took an element
- Repeat until one array is exhausted
- Append all remaining elements from the non-empty array
Why This Works
At each step, we maintain this invariant: the output contains the smallest i + j elements overall, in sorted order.
Initially this holds because the output is empty. At each step, since both arrays are sorted, every element we haven't yet taken is at least as large as the current front element of its array. Therefore, the minimum of the two front elements is the smallest remaining element overall. Taking it preserves sorted order in the output.
When one array runs out, every remaining element sits in the other array. That remaining portion is already sorted, and its first element was never smaller than anything we've already taken. Appending it preserves the sorted property.
For ties, using <= ensures we take from the first array before the second array, maintaining stable ordering.
Complexity
Time: O(n + m) where n and m are the lengths of the two input arrays. We examine each element exactly once. The main loop runs at most n + m iterations, and the final extend operations handle whatever remains.
Space: O(n + m) for the output array, which is unavoidable since we must return a new array. Working space beyond the output is O(1), using only two index variables.
Solution
from typing import List
def combine_sorted_arrays(first: List[int], second: List[int]) -> List[int]:
i = j = 0
result = []
while i < len(first) and j < len(second):
if first[i] <= second[j]:
result.append(first[i])
i += 1
else:
result.append(second[j])
j += 1
result.extend(first[i:])
result.extend(second[j:])
return result