https www enjoyalgorithms comblogmedianoftwosortedarrays find the median of two arrays args nums1 the first array nums2 the second array returns the median of the two arrays examples findmediansortedarrays1 3 2 2 0 findmediansortedarrays1 2 3 4 2 5 findmediansortedarrays0 0 0 0 0 0 findmediansortedarrays traceback most recent call last valueerror both input arrays are empty findmediansortedarrays 1 1 0 findmediansortedarrays1000 1000 0 0 findmediansortedarrays1 1 2 2 3 3 4 4 2 75 merge the arrays into a single sorted array if the total number of elements is even calculate the average of the two middle elements as the median find the median of two arrays args nums1 the first array nums2 the second array returns the median of the two arrays examples find_median_sorted_arrays 1 3 2 2 0 find_median_sorted_arrays 1 2 3 4 2 5 find_median_sorted_arrays 0 0 0 0 0 0 find_median_sorted_arrays traceback most recent call last valueerror both input arrays are empty find_median_sorted_arrays 1 1 0 find_median_sorted_arrays 1000 1000 0 0 find_median_sorted_arrays 1 1 2 2 3 3 4 4 2 75 merge the arrays into a single sorted array if the total number of elements is odd then return the middle element if the total number of elements is even calculate the average of the two middle elements as the median
def find_median_sorted_arrays(nums1: list[int], nums2: list[int]) -> float:
    
    if not nums1 and not nums2:
        raise ValueError("Both input arrays are empty.")

    
    merged = sorted(nums1 + nums2)
    total = len(merged)

    if total % 2 == 1:  
        return float(merged[total // 2])  

    
    
    middle1 = merged[total // 2 - 1]
    middle2 = merged[total // 2]
    return (float(middle1) + float(middle2)) / 2.0


if __name__ == "__main__":
    import doctest

    doctest.testmod()