Merge Sorted Array
Different approaches to solve Leetcode 88 in JavaScript

There are countless ways to approach this problem and optimize your solution. In this article, we’ll explore two strategies to tackle this problem. Let’s see the problem statement first.
Problem Statement:
You are given two integer arrays
nums1andnums2, sorted in non-decreasing order, and two integersmandn, representing the number of elements innums1andnums2respectively.Merge
nums1andnums2into a single array sorted in non-decreasing order.The final sorted array should not be returned by the function, but instead be stored inside the array
nums1. To accommodate this,nums1has a length ofm + n, where the firstmelements denote the elements that should be merged, and the lastnelements are set to0and should be ignored.nums2has a length ofn.
Approach 1: Brute Force
The simplest approach is to create a new array, iterate over both input arrays, and insert each element into the new array in sorted order.
The function first creates an empty
mergedarray to hold the merged and sorted elements. It then initializes two pointersiandjto point to the first elements ofnums1andnums2, respectively.The function then enters a while loop that compares the elements at
nums1[i]andnums2[j]and adds the smaller element to themergedarray using thepushmethod. The loop continues until one of the pointers reaches the end of its corresponding array.After the loop, there may be some remaining elements in either
nums1ornums2. The function copies these remaining elements intomergedusing two additional while loops.Finally, the function copies the sorted elements in
mergedback intonums1using a for loop that iterates over the elements inmergedand assigns them to the corresponding indices innums1.
// ES6 Arrow Function
const merge = (nums1, m, nums2, n) => {
let merged = [];
let i = 0, j = 0;
while(i < m && j < n) {
if(nums1[i] <= nums2[j]) {
merged.push(nums1[i]);
i++;
} else {
merged.push(nums2[j]);
j++;
}
}
while(i < m) {
merged.push(nums1[i]);
i++;
}
while(j < n) {
merged.push(nums2[j]);
j++;
}
for(let k = 0; k < merged.length; k++) {
nums1[k] = merged[k];
}
}
Time Complexity: O((m + n) log(m + n))
Space Complexity: O(m + n)
Approach 2: Two-Pointers
This is a more optimized approach that involves using two pointers, one for each input array, to merge the arrays in place.
Start at the end of both input arrays and work your way backwards, comparing the values at each pointer and inserting the larger value at the end of the first input array.
// ES6 Arrow Function
const merge = (nums1, m, nums2, n) => {
let i = m - 1;
let j = n - 1;
let k = m + n - 1;
while(i >= 0 && j >= 0) {
if(nums1[i] >= nums2[j]) {
nums1[k] = nums1[i];
i--;
} else {
nums1[k] = nums2[j];
j--;
}
k--;
}
while(j >= 0) {
nums1[k] = nums2[j];
j--;
k--;
}
}
Time Complexity: O(m + n)
Space Complexity: O(1)
And there you have it guys! I hope this article has provided you with valuable insights and helped you better understand the different approaches to solving this problem. Happy coding!
Problem - Leetcode 88




