WHY NOT IT IS PASSING ALL THE TEST CASES
MEDIAN OF SORTED ARRAYS
Check for test case:
4
1 2 3 6
4 6 8 10
Expected output is: 5
Yours is giving is: 4
Here is the approach:
- 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 also refer to simple approach like this
If you find above one little complex.
why can not we use this approach , fiest merge the two arrays and then find the median of the merged array
We are using this only, but your array is not merged your c array is like this
1 4 2 6 3 8 6 10
Instead it should have been
1 2 3 4 6 6 8 10
Now find the median of this array
This is the approach you should apply.
ok thank you so much
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.