Categories
Binary Search Recursion

Second highest number between two arrays

let secondHighest = function(arr1, arr2) {

    if (arr1.length === 1 && arr2.length === 1) {
        return Math.min(arr1[0], arr2[0]);
    }
    else if (arr1.length === 0 && arr2.length === 2) {
        return Math.min(arr2[0], arr2[1]);
    }
    else if (arr2.length === 0 && arr1.length === 2) {
        return Math.min(arr1[0], arr1[1]);
    }

    let mid1 = Math.ceil((arr1.length / 2) - 1)
    let mid2 = Math.ceil((arr2.length / 2) - 1);

    if (arr1[mid1] <= arr2[mid2]) {

        arr1 = arr1.slice(mid1 + 1);
        arr2 = arr2.slice(mid2);
    }
    else {
        arr1 = arr1.slice(mid1);
        arr2 = arr2.slice(mid2 + 1);
    }

    return secondHighest(arr1, arr2);
}

Leave a Reply

Your email address will not be published. Required fields are marked *