Median of 2 Sorted Arrays of Equal Size using JavaScript
Last Updated :
03 Apr, 2024
The median is the middle element of the array. If several elements are odd then the middle element is the median and if several elements are even then the average of the two middle values is the median after arranging the array in sorted order.
Below are the approaches to find the median of 2 sorted arrays of equal size using JavaScript:
Using Simple merging
Merge the two given sorted arrays and sort them using the sort() method. If the length of the merged array is even then return the middle element as the median else if the length is even then return the average of the two middle elements.
Example: To demonstrate finding the Median of 2 sorted arrays of equal size using merging
JavaScript
function MedianOfSortedArrays(nums1, nums2) {
const mergedArray = nums1
.concat(nums2)
.sort((a, b) => a - b);
const n = mergedArray.length;
if (n % 2 === 0) {
return (mergedArray[n / 2 - 1] + mergedArray[n / 2]) / 2;
} else {
return mergedArray[Math.floor(n / 2)];
}
}
const nums1 = [1, 2, 3];
const nums2 = [4];
console.log(
"Median of 2 sorted array is :", MedianOfSortedArrays(nums1, nums2));
OutputMedian of 2 sorted array is : 2.5
Time Complexity: O(m + n) , m and n are size of input array.
Space Complexity: O(1) , constant space.
Using Modified Binary Search
We will first check if lengths of array are equal or not. Then we apply Binary search to find the correct position to partition the arrays. Now we find the maximum and minimum values on the left and right sides of the partition for both arrays and Check conditions for median if the total number of elements is even or odd. Return the median value accordingly . If condition is not met we Adjust partition based on conditions and Repeat the binary search process until median is found. When the correct partition is found, return the calculated median value.
Example: To demonstrate finding Median of 2 sorted arrays of equal size using merging
JavaScript
function findMedianSortedArrays(nums1, nums2) {
const m = nums1
.length;
const n = nums2
.length;
if (m > n) return findMedianSortedArrays(nums2, nums1);
let low = 0;
let high = m;
while (low <= high) {
const partitionX = Math
.floor((low + high) / 2);
const partitionY = Math
.floor((m + n + 1) / 2) - partitionX;
const maxX = partitionX === 0 ?
Number.NEGATIVE_INFINITY : nums1[partitionX - 1];
const maxY = partitionY === 0 ?
Number.NEGATIVE_INFINITY : nums2[partitionY - 1];
const minX = partitionX === m ?
Number.POSITIVE_INFINITY : nums1[partitionX];
const minY = partitionY === n ?
Number.POSITIVE_INFINITY : nums2[partitionY];
if (maxX <= minY && maxY <= minX) {
if ((m + n) % 2 === 0) {
return (
Math
.max(maxX, maxY)
+
Math
.min(minX, minY)) / 2;
} else {
return Math.max(maxX, maxY);
}
} else if (maxX > minY) {
high = partitionX - 1;
} else {
low = partitionX + 1;
}
}
}
const nums1 = [1, 2, 3];
const nums2 = [4];
console.log(findMedianSortedArrays(nums1, nums2));
Time Complexity: O(log(min(m,n)
Space Complexity: O(1)
Similar Reads
Median of Two Sorted Arrays of Different Sizes using JavaScript Given two arrays, our goal is to find the median of two sorted arrays of different sizes by merging both of them. The median is the middle value of the array after dividing it into two halves. Example: Input: Arr1: [1,4,5], Arr2: [2,8,7,3]Output: Median = 4Explanation: The merged and sorted array is
4 min read
Merge K Sorted Array using JavaScript We are given K sorted arrays and our task is to merge them into a single sorted array in JavaScript.Example:Input: arrays = [[2, 4, 5], [1, 3, 8], [0, 7, 9]];Output: [0, 1, 2, 3, 4, 5, 7, 8]These are the following approaches:Table of ContentNaive ApproachDivide and Conquer ApproachMin-Heap ApproachN
5 min read
JavaScript - Sort an Array of Strings Here are the various methods to sort an array of strings in JavaScript1. Using Array.sort() MethodThe sort() method is the most widely used method in JavaScript to sort arrays. By default, it sorts the strings in lexicographical (dictionary) order based on Unicode values.JavaScriptlet a = ['Banana',
3 min read
JavaScript- Merge two Sorted Arrays These are the following ways to merge two sorted arrays: 1. Using Two Pointers (Efficient For Large Arrays)This approach involves using two pointers, one for each array, and comparing their elements as you iterate through them. This method works efficiently in O(n + m) time, where n and m are the le
3 min read
Sorting Array of Number by Increasing Frequency using JavaScript To sort an array of given numbers based on the frequency of occurrence of each number first sort the array such that the elements repeated for the least number of times appear first followed by the elements with increasing frequency. Example: Input:array = [3, 3, 1, 1, 1, 8, 3, 6, 8, 8] Output:[6, 1
3 min read
How to get median of an array of numbers in JavaScript ? In this article, we will see how to find the median of an array using JavaScript. A median is a middle value in a given set of numbers or data. Examples: Input arr1 : [1 4 7 9]Output : 5.5Explanation : The median of the two sorted array is the middle elements which is 5 if the arrayLength =arr1.leng
3 min read