Introduction π
If you're preparing for coding interviews, especially for roles in FAANG or similar companies, LeetCode's "Merge Sorted Array" problem is a must-solve! This problem tests your understanding of in-place array manipulations and merging algorithms. Letβs dive into the problem, understand it step-by-step, and solve it with Java.
Problem Statement π
You are given two integer arrays, nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.
Merge nums1 and nums2 into a single sorted array in-place. The merged array should overwrite nums1 while maintaining sorted order.
π‘ Examples
Example 1
Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3
Output:
[1,2,2,3,5,6]
Explanation: The arrays to merge are [1,2,3] and [2,5,6].
Example 2
Input:
nums1 = [1], m = 1
nums2 = [], n = 0
Output:
[1]
Example 3
Input:
nums1 = [0], m = 0
nums2 = [1], n = 1
Output:
[1]
Thought Process π§
To solve this in-place, we can use a two-pointer technique. Instead of starting from the beginning, we begin at the end of both arrays. Why? Because the largest numbers will occupy the last positions in nums1. Here's how it works:
- Use two pointers,
iandj, initialized tom - 1(end ofnums1's actual values) andn - 1(end ofnums2). - Compare
nums1[i]andnums2[j]. Place the larger of the two at the last position ofnums1. - Decrement the respective pointer and repeat.
- If any elements remain in
nums2, copy them tonums1.
Java Solution π‘
Here's the clean and optimized solution:
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int i = m - 1;
int j = n - 1;
int k = m + n - 1;
while (i >= 0 && j >= 0) {
if (nums1[i] > nums2[j]) {
nums1[k--] = nums1[i--];
} else {
nums1[k--] = nums2[j--];
}
}
while (j >= 0) {
nums1[k--] = nums2[j--];
}
}
}
Explanation of the Code π
-
Pointers Initialization:
-
ipoints to the last valid element innums1. -
jpoints to the last element innums2. -
kpoints to the last position innums1.
-
-
Reverse Traversal:
- Compare
nums1[i]andnums2[j]. Place the larger value atnums1[k]. - Decrement
kand the pointer (iorj) of the array from which the element was taken.
- Compare
-
Remaining Elements in
nums2:- If
nums2still has elements left (whenj >= 0), copy them intonums1.
- If
Note: If nums1 runs out of elements (i < 0), we only need to worry about nums2, as nums1 is already in place.
Why This Solution Works π§
-
Time Complexity:
O(m + n)We iterate through both arrays once.
-
Space Complexity:
O(1)No extra space is used; the merge is done in-place.
Key Takeaways π―
- Understand Two-Pointer Technique: Crucial for merging or comparing arrays in sorted order.
- Think In-Place: Many interview problems require optimizing space complexity.
- Practice Edge Cases: Arrays with zero elements, fully overlapping elements, or extreme sizes.
Conclusion β¨
Solving problems like Merge Sorted Array is essential for mastering array manipulations and building a strong foundation for coding interviews. If you enjoyed this post or have any questions, feel free to comment below. Happy coding! π

Top comments (1)
Follow Me on GitHub π
If you found this solution helpful, check out more of my projects and solutions on my GitHub profile.
Don't forget to follow for more updates!