Implement the findMedianOfTwoSortedArraysSimple method that returns the floor of the median after combining two sorted arrays.

The input contains two sorted integer arrays nums1 and nums2, along with their lengths size1 and size2. Your task is to find the median value after both arrays are merged.

Because the return type is int, when the total number of values is even, return the lower middle value. For example, merging [1,2] and [3,4] gives [1,2,3,4], so the lower middle value is 2.

Example 1

Input
nums1 (int[]) = [1,3]
size1 (int) = 2
nums2 (int[]) = [2]
size2 (int) = 1
Returns (int) 2

Example 2

Input
nums1 (int[]) = [1,2]
size1 (int) = 2
nums2 (int[]) = [3,4]
size2 (int) = 2
Returns (int) 2

Example 3

Input
nums1 (int[]) = [0,0]
size1 (int) = 2
nums2 (int[]) = [0,0]
size2 (int) = 2
Returns (int) 0

Merge both sorted arrays into one sorted list.

After merging, find the median index. For an odd total length, it is the normal middle index. For an even total length, use the lower middle index, which is (total - 1) / 2.

Return the value stored at that index.

Pseudocode:

function findMedianOfTwoSortedArraysSimple(nums1, size1, nums2, size2):
    merged = empty list
    i = 0
    j = 0
    while i < size1 and j < size2:
        if nums1[i] <= nums2[j]:
            add nums1[i] to merged
            i++
        else:
            add nums2[j] to merged
            j++
    while i < size1:
        add nums1[i] to merged
        i++
    while j < size2:
        add nums2[j] to merged
        j++
    medianIndex = (size1 + size2 - 1) / 2
    return merged[medianIndex]
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.