Merge Sorted Array
You are given two integer arrays nums1nums2mnnums1nums2
Merge nums1nums2
The final sorted array should not be returned by the function, but instead be stored inside the array nums1nums1m + nmn0nums2n
Example 1
Output: [1,2,2,3,5,6]
Explanation: The arrays we are merging are [1,2,3] and [2,5,6].
The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1.
Example 2
Output: [1]
Explanation: The arrays we are merging are [1] and [].
The result of the merge is [1].
Example 3
Output: [1]
Explanation: The arrays we are merging are [] and [1]. The result of the merge is [1].
Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result can fit in nums1.
Constraints:
nums1.length == m + nnums2.length == n0 <= m, n <= 2001 <= m + n <= 200-109 <= nums1[i], nums2[j] <= 109
Follow up: Can you come up with an algorithm that runs in O(m + n)
Solution 1 - Start filling nums1 array from right side with higher elements
As per the problem statement, input arrays nums1nums2nums1nums2mnums1nnums2nums1 = [4, 5, 6, 0, 0, 0]nums2 = [1, 2, 3]
Now, we have to fill elements from nums2nums1
So, in this approach, we will starting filling nums1nums1nums2
Implementation steps:
-
As a first step, We will create a variable
and initialize with last index oflast array,nums1 . We will also utilize inputslast = nums1.length -1; andm as two pointers to point ton andnums1 respectively.nums2 -
Then, while
andm > 0 , we will check the elements fromn > 0 andnums1 and copy the highest element tonums2 from right side. If we copy element fromnums1 , decrementnums1 , or if we copy element fromm , decrementnums2 .n -
At the end, we may still have some elements in
, just like above examplenums2 andnums1 = [4, 5, 6, 0, 0, 0] , sincenums2 = [1, 2, 3] have highest elements, we will copy all elements fromnums1 , and when we exit thenums1 loop, we will have elements left inwhile , so we will simply copy all remaining elements fromnums2 .nums2
import java.util.Arrays;
public class MergeSortedArray {
static void merge(int[] nums1, int m, int[] nums2, int n) {
int last = nums1.length - 1;
while(m>0 && n>0) {
if(nums1[m-1] > nums2[n-1]) {
nums1[last--] = nums1[m-1];
m--;
} else {
nums1[last--] = nums2[n-1];
n--;
}
}
while(n>0) {
nums1[last--] = nums2[n-1];
n--;
}
}
public static void main(String[] args) {
int[] nums1 = {1,2,3,0,0,0};
int[] nums2 = {2,5,6};
merge(nums1, 3, nums2, 3);
System.out.println(Arrays.toString(nums1));
nums1 = new int[]{1};
nums2 = new int[]{};
merge(nums1, 1, nums2, 0);
System.out.println(Arrays.toString(nums1));
nums1 = new int[]{0};
nums2 = new int[]{1};
merge(nums1, 1, nums2, 1);
System.out.println(Arrays.toString(nums1));
nums1 = new int[]{4,5,6,0,0,0};
nums2 = new int[]{1,2,3};
merge(nums1, 3, nums2, 3);
System.out.println(Arrays.toString(nums1));
}
}
Complexity Analysis:
Time complexity: Above code runs in O(m + n) time where mnums1nnums2
Space complexity: O(1).
Above implementations source code can be found at