![]() |
VOOZH | about |
Given two sorted arrays a[] and b[] of size n and m respectively, merge both the arrays and rearrange the elements such that the smallest n elements are in a[] and the remaining m elements are in b[]. All elements in a[] and b[] should be in sorted order.
Examples:
Input: a[] = [2, 4, 7, 10], b[] = [2, 3]
Output: a[] = [2, 2, 3, 4], b[] = [7, 10]
Explanation: Combined sorted array = [2, 2, 3, 4, 7, 10], array a[] contains smallest 4 elements: 2, 2, 3 and 4, and array b[] contains remaining 2 elements: 7, 10.Input: a[] = [1, 5, 9, 10, 15, 20], b[] = [2, 3, 8, 13]
Output: a[] = [1, 2, 3, 5, 8, 9], b[] = [10, 13, 15, 20]
Explanation: Combined sorted array = [1, 2, 3, 5, 8, 9, 10, 13, 15, 20, array a[] contains smallest 6 elements: 1, 2, 3, 5, 8 and 9, and array b[] contains remaining 4 elements: 10, 13, 15, 20.Input: a[] = [0, 1], b[] = [2, 3]
Output: a[] = [0, 1], b[] = [2, 3]
Explanation: Combined sorted array = [0, 1, 2, 3], array a[] contains smallest 2 elements: 0 and 1, and array b[] contains remaining 2 elements: 2 and 3.
Table of Content
The idea is to traverse b[] from the end in reverse order and compare each element with the last (largest) element of a[]. For any index i, if b[i] is smaller than the last element of a[], replace b[i] with the last element of a[] and use insert step of insertion sort to find the correct place of b[i] in a[].
How do we keep a[] sorted? Every time we add any element from b[] to a[], we find the correct index using insert step of insertion sort.
How do we keep b[] sorted? This is ensured by the fact that we traverse b[] from end and insert only when current element of b[] is smaller.
Illustration
1 2 3 5 8 9 10 13 15 20
Time Complexity: O(m * n), where n and m are sizes of a[] and b[] respectively.
Auxiliary Space: O(1)
We can use the fact that nth smallest element in the sorted combined array acts as a pivot dividing the elements among a[] and b[]. Initially, this nth smallest element can lie in either arrays so instead of finding it, we can find the first index idx in a[], such that the elements after this index were larger than the pivot.
Elements of a[] placed after index idx should be replaced with smaller elements from b[]. Now all elements were in the correct arrays and we can apply sorting to both arrays to maintain the order.
1 2 3 5 8 9 10 13 15 20
Time Complexity: O(log(m) + m*log(m) + n*log(n)), where n and m are sizes of a[] and b[] respectively.
Auxiliary Space: O(1)
Further Optimization: We can optimize the first step to O(log(min(m, n))) by picking the smaller array for binary search.
Actually we can use the previous approach without finding the pivot index. We just need to swap the rightmost element in a[] with leftmost element in b[], then second rightmost element in a[] with second leftmost element in b[] and so on. This will continue until the selected element from a[] is larger than selected element from b[].
When we reach at the pivot index this condition fails automatically and we will stop here. Now sort both the arrays to maintain the order.
1 2 3 5 8 9 10 13 15 20
Time Complexity: O((m+n) + m*log(m) + n*log(n)), where n and m are sizes of a[] and b[] respectively.
Auxiliary Space: O(1)
The idea is to assume the two arrays as a single continuous array of size n + m and sort it using gap method of shell sort. Here we need to adjust the indices according to whether it lies in a[] or b[]. And if indices lies in b[] then adjust the indices by subtracting n from it.
1 2 3 5 8 9 10 13 15 20
Time Complexity: O(m+n)*O(log(m+n)), the outer loop runs from m+n to 1 and its everytime divided by 2. So, outer loop complexity is O(log(m+n)). The inner loop time complexity is O(m+n).
Auxiliary Space : O(1)
Merge two sorted arrays
Merge k sorted arrays | Set 1
Efficiently merging two sorted arrays with O(1) extra space