Hi binary search approach is not clear, please explain with example.
Binary Search Approach
@dev_emma hey,here is logic for that search
If m1 and m2 both are median of 2 arrays and 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
hope you get it,try coding this approach.
Happy coding
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.