Given two sorted vectors, combine their elements into a single vector that is also sorted.
Your implementation should remain efficient when the vectors have different lengths and should correctly handle edge cases such as one or both inputs being empty.
std::vector<int> mergeSortedVectors(const std::vector<int>& a, const std::vector<int>& b);
Input:
1 3 5 7
2 4 6 8
Output:
1 2 3 4 5 6 7 8
Explanation: Interleaving the two sorted sequences produces one sequence with all values in ascending order.
Input:
2 5 9
1 3 3 8
Output:
1 2 3 3 5 8 9
Explanation: The duplicate value 3 is preserved, and all values are merged into sorted order.
Input:
4 6
(empty)
Output:
4 6
Explanation: When one vector is empty, the merged result is simply the other vector.