Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Median of Two Sorted Arrays

const findMedianSortedArrays = (num1, num2) => {

    let total = num1.length + num2.length
    let arr = []

    if (total === 1) {
        return num1.length == 1 ? num1[0] : num2[0];
    }

    let arr_len = total % 2 == 0 ? (total) / 2 + 1 : Math.ceil(total / 2);
    let i = 0
    let j = 0
    while (arr.length < arr_len) {
        if (i < num1.length && j < num2.length) {
            if (num1[i] <= num2[j]) {
                arr.push(num1[i])
                i++
            } else {
                arr.push(num2[j])
                j++
            }
        } else if (i >= num1.length) {
            arr.push(num2[j])
            j++

        } else {
            arr.push(num1[i])
            i++
        }

   

    }
    return total % 2 == 0 ? (arr[arr.length - 1] + arr[arr.length - 2]) / 2 : arr[arr.length - 1];
}
 
PREVIOUS NEXT
Tagged: #Median #Two #Sorted #Arrays
ADD COMMENT
Topic
Name
3+6 =