Leetcode: 4. Find Median of Two Sorted Arrays

Muhaymin Bin Mehmood

Muhaymin Bin Mehmood

· 2 min read
Find Median of Two Sorted Arrays Banner Image
Find Median of Two Sorted Arrays Banner Image

Problem:
Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

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.

Function Definition

function findMedianSortedArrays(nums1: number[], nums2: number[]): number {

Explanation:

  • This line defines a function named findMedianSortedArrays.
  • It takes two parameters: nums1, an array of numbers, and nums2, another array of numbers.
  • It returns a number, which is presumably the median of the merged arrays.

Merging and Sorting

let mergeArr = [];
let findMedian = 0;
let medianArr = [];
let findMedianLength = 0;
mergeArr.push(...nums1, ...nums2);
const sortedArr = mergeArr.sort((a, b) => a - b);

Explanation:

  • We initialize four variables: mergeArr to store the merged arrays, findMedian to store the length of mergeArr, medianArr to hold middle elements (if needed), and findMedianLength to store the calculated median.
  • We merge nums1 and nums2 arrays into mergeArr using the push() method and the spread syntax (...).
  • We sort mergeArr in ascending order using the sort() method. The comparison function (a, b) => a - b ensures numerical sorting.

Determining Median

findMedian = sortedArr.length;
const isEven = sortedArr.length % 2 === 0;

Explanation:

  • We set findMedian to the length of sortedArr.
  • We determine whether the length of sortedArr is even by checking if the remainder of division by 2 equals 0.

Handling Even Length

if (isEven) {
    medianArr.push(mergeArr[Math.floor(findMedian / 2) - 1], mergeArr[Math.floor(findMedian / 2)]);
    for (let i = 0; i < medianArr.length; i++) {
        if (!isNaN(medianArr[i + 1])) {
            findMedianLength = (medianArr[i] + medianArr[i + 1]) / 2;
        }
    }
}

Explanation:

  • If sortedArr has an even length:
    • We calculate the two middle elements and push them into medianArr.
    • We iterate over medianArr to find the middle two elements and calculate their average.

Handling Odd Length

else {
    medianArr.push(mergeArr[Math.ceil(findMedian / 2 - 1)]);
    const convertMedianArr = medianArr.join('');
    findMedianLength = Number(convertMedianArr);
}

Explanation:

  • If sortedArr has an odd length:
    • We calculate the single middle element and push it into medianArr.
    • We convert medianArr to a string, then parse it back to a number and assign it to findMedianLength.

Returning Median

return findMedianLength;

Explanation

  • We return the calculated median value.

Full Code:

function findMedianSortedArrays(nums1: number[], nums2: number[]): number {
    let mergeArr = [];
    let findMedian = 0;
    let medianArr = [];
    let findMedianLength = 0;
    mergeArr.push(...nums1, ...nums2)
    const sortedArr = mergeArr.sort((a, b) => a - b);
    findMedian = sortedArr.length;
    const isEven = sortedArr.length % 2 === 0
    if(isEven) {
        medianArr.push(mergeArr[Math.floor(findMedian / 2) - 1], mergeArr[Math.floor(findMedian / 2)])
        for(let i = 0; i < medianArr.length; i++){
            if(!isNaN(medianArr[i + 1])){
                findMedianLength = (medianArr[i] + medianArr[i + 1]) / 2;
            }
        }
    }else {
        medianArr.push(mergeArr[Math.ceil(findMedian / 2 - 1)])
        const convertMedianArr = medianArr.join('')
        findMedianLength = Number(convertMedianArr)
    }
    return findMedianLength
};


Muhaymin Bin Mehmood

About Muhaymin Bin Mehmood

I am a Front-End Developer specializing in Frontend at Dvago.pk.

Copyright © 2024 Mbloging. All rights reserved.