Problem:
Given two sorted arrays nums1
and nums2
of size m
and n
respectively, return the median of the two sorted arrays.
The overall run time complexity should be O(log (m+n))
.
Example 1:
Input: nums1 = [1,3], nums2 = [2] Output: 2.00000 Explanation: merged array = [1,2,3] and median is 2.
Example 2:
Input: nums1 = [1,2], nums2 = [3,4] Output: 2.50000 Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
Constraints:
nums1.length == m
nums2.length == n
0 <= m <= 1000
0 <= n <= 1000
1 <= m + n <= 2000
-106 <= nums1[i], nums2[i] <= 106
Solution:
Solution with separate function to merge two arrays in O(n) complexity.
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number}
*/
var findMedianSortedArrays = function(nums1, nums2) {
let newarray = new Array();
let lengthh = (nums1.length > nums2.length)?nums1.length:nums2.length;
for(let i = 0; i<lengthh; i++){
if(nums1[i] || nums2[i]){
if(nums1[i] > nums2[i]){
if(newarray[newarray.length - 1] > nums2[i]){
let buff = newarray[newarray.length - 1];
newarray.pop();
newarray.push(nums2[i]);
newarray.push(buff);
}else{
newarray.push(nums2[i]);
}
if(newarray[newarray.length - 1] > nums1[i]){
let buff = newarray[newarray.length - 1];
newarray.pop();
newarray.push(nums1[i]);
newarray.push(buff);
}else{
newarray.push(nums1[i]);
}
}
else{
if(newarray[newarray.length - 1] > nums1[i]){
let buff = newarray[newarray.length - 1];
newarray.pop();
newarray.push(nums1[i]);
newarray.push(buff);
}else{
newarray.push(nums1[i]);
}
if(newarray[newarray.length - 1] > nums2[i]){
let buff = newarray[newarray.length - 1];
newarray.pop();
newarray.push(nums2[i]);
newarray.push(buff);
}else{
newarray.push(nums2[i]);
}
}
}
}
newarray = mergeTwo(nums1, nums2)
newarray = newarray.filter(function( element ) {
return element !== undefined;
});
if(newarray.length % 2){
if(newarray.length){
return newarray[Math.ceil(newarray.length/2 -1)];
}
else{
newarray[0];
}
}
else{
if(newarray[newarray.length/2 - 1] || newarray[newarray.length/2]){
return (newarray[newarray.length/2 - 1] + newarray[newarray.length/2])/2;
}
else {
return 0;
}
}
};
// O(n) time & O(n) space
function mergeTwo(arr1, arr2) {
let merged = [];
let index1 = 0;
let index2 = 0;
let current = 0;
while (current < (arr1.length + arr2.length)) {
let isArr1Depleted = index1 >= arr1.length;
let isArr2Depleted = index2 >= arr2.length;
if (!isArr1Depleted && (isArr2Depleted || (arr1[index1] < arr2[index2]))) {
merged[current] = arr1[index1];
index1++;
} else {
merged[current] = arr2[index2];
index2++;
}
current++;
}
return merged;
}
A freelance web developer with a decade of experience in creating high-quality, scalable web solutions. His expertise spans PHP, WordPress, Node.js, MySQL, MongoDB, and e-commerce development, ensuring a versatile approach to each project. Aadi’s commitment to client satisfaction is evident in his track record of over 200 successful projects, marked by innovation, efficiency, and a customer-centric philosophy.
As a professional who values collaboration and open communication, Aadi has built a reputation for delivering projects that exceed expectations while adhering to time and budget constraints. His proactive and problem-solving mindset makes him an ideal partner for anyone looking to navigate the digital landscape with a reliable and skilled developer.