Searching & SortingProblem 14 of 36
Merge 2 sorted arrays
Problem Statement
Given two sorted arrays, merge them into a single sorted array.
Constraints:
- Both arrays are already sorted in ascending order
- Arrays may have different sizes
- Elements may be duplicated
Example:
- Input:
arr1 = [1, 3, 5, 7],arr2 = [2, 4, 6, 8] - Output:
[1, 2, 3, 4, 5, 6, 7, 8]
Example 2:
- Input:
arr1 = [1, 1, 2],arr2 = [1, 3] - Output:
[1, 1, 1, 2, 3]
Approach 1: Brute Force (Concatenate and Sort)
Intuition
Simply concatenate both arrays and sort the result.
Algorithm
- Create a new array of size m + n
- Copy all elements from both arrays
- Sort the combined array
java
import java.util.Arrays;
public class Solution {
public int[] merge(int[] arr1, int[] arr2) {
int[] result = new int[arr1.length + arr2.length];
// Copy all elements
int idx = 0;
for (int num : arr1) {
result[idx++] = num;
}
for (int num : arr2) {
result[idx++] = num;
}
// Sort
Arrays.sort(result);
return result;
}
}Complexity Analysis
- Time Complexity: O((m+n) log(m+n)) - Sorting dominates
- Space Complexity: O(m+n) - Result array
Approach 2: Two Pointers (Optimal)
Intuition
Use two pointers, one for each array. Compare elements and add the smaller one to the result. This takes advantage of the fact that both arrays are already sorted.
Algorithm
- Initialize two pointers i = 0, j = 0
- While both pointers are within bounds:
- Compare arr1[i] and arr2[j]
- Add smaller element to result and advance that pointer
- Add remaining elements from non-empty array
java
public class Solution {
public int[] merge(int[] arr1, int[] arr2) {
int m = arr1.length;
int n = arr2.length;
int[] result = new int[m + n];
int i = 0, j = 0, k = 0;
// Compare and merge
while (i < m && j < n) {
if (arr1[i] <= arr2[j]) {
result[k++] = arr1[i++];
} else {
result[k++] = arr2[j++];
}
}
// Copy remaining elements from arr1
while (i < m) {
result[k++] = arr1[i++];
}
// Copy remaining elements from arr2
while (j < n) {
result[k++] = arr2[j++];
}
return result;
}
}Dry Run Example
arr1 = [1, 3, 5, 7], arr2 = [2, 4, 6, 8]
i=0, j=0: arr1[0]=1 < arr2[0]=2, add 1, i=1
i=1, j=0: arr1[1]=3 > arr2[0]=2, add 2, j=1
i=1, j=1: arr1[1]=3 < arr2[1]=4, add 3, i=2
i=2, j=1: arr1[2]=5 > arr2[1]=4, add 4, j=2
i=2, j=2: arr1[2]=5 < arr2[2]=6, add 5, i=3
i=3, j=2: arr1[3]=7 > arr2[2]=6, add 6, j=3
i=3, j=3: arr1[3]=7 < arr2[3]=8, add 7, i=4
j=3: add remaining: 8
Result: [1, 2, 3, 4, 5, 6, 7, 8]
Complexity Analysis
- Time Complexity: O(m+n) - Each element visited once
- Space Complexity: O(m+n) - Result array
Approach 3: Merge into arr1 (In-place for LeetCode variant)
When arr1 has enough space to hold all elements.
java
public class Solution {
public void merge(int[] arr1, int m, int[] arr2, int n) {
int i = m - 1; // Last element in arr1
int j = n - 1; // Last element in arr2
int k = m + n - 1; // Last position in merged array
// Merge from the end
while (i >= 0 && j >= 0) {
if (arr1[i] > arr2[j]) {
arr1[k--] = arr1[i--];
} else {
arr1[k--] = arr2[j--];
}
}
// Copy remaining elements from arr2
while (j >= 0) {
arr1[k--] = arr2[j--];
}
}
}Why Merge from End?
- If we merge from the start, we'd overwrite elements in arr1 before using them
- Merging from the end ensures we fill the empty spaces first
- No elements are overwritten before they're used
Complexity Analysis
- Time Complexity: O(m+n) - Each element visited once
- Space Complexity: O(1) - In-place merge
Merging K Sorted Arrays
Using Min-Heap
Complexity for K Arrays
- Time Complexity: O(N log K) where N is total elements
- Space Complexity: O(K) for the heap
Key Takeaways
- Two Pointer Technique: Efficient for merging sorted arrays
- Merge from End: When merging in-place to avoid overwriting
- Stability: The merge is stable (maintains relative order of equal elements)
- Foundation of Merge Sort: This is the merge step of merge sort
- K-way Merge: Use min-heap for merging K sorted arrays
- Applications: External sorting, database merge joins, stream processing