Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.
The overall run time complexity should be O(log (m+n)).
Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Explanation: merged array = [1,2,3] and median is 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.
The simplest approach to find the median of two sorted arrays is to merge them into a single sorted array, then directly find the median. This approach leverages the idea of merging two arrays, similar to the merge process in merge sort.
A more efficient approach uses binary search. Instead of merging the entire arrays, we use binary search on the shorter array to partition the two arrays into two halves such that all elements on the left side are less than or equal to all elements on the right side. This way, we can determine the median in O(log(min(m, n))) time.