I am not able to get a slight hint of the 1st approach of binary search which he is discussing in this video.
I didn't get his first approach of using binary search
First getting medians of the two sorted arrays and then comparing them.
- Calculate the medians m1 and m2 of the input arrays ar1[]
and ar2[] respectively. - If m1 and m2 both are equal then we are done.
return m1 (or m2) - If m1 is greater than m2, then median is present in one
of the below two subarrays.
a) From first element of ar1 to m1 (ar1[0…|n/2|])
b) From m2 to last element of ar2 (ar2[|n/2|…n-1]) - If m2 is greater than m1, then median is present in one
of the below two subarrays.
a) From m1 to last element of ar1 (ar1[|n/2|…n-1])
b) From first element of ar2 to m2 (ar2[0…|n/2|]) - Repeat the above process until size of both the subarrays
becomes 2. - If size of the two arrays is 2 then use below formula to get
the median.
Median = (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1]))/2
You can have a look at this implementation.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.