im having a question from interview bit on binary search :-
https://www.interviewbit.com/problems/median-of-array/.
can anyone will explain this problem solution step by step
with code.
plz, help me im very confused .
im having a question from interview bit on binary search :-
https://www.interviewbit.com/problems/median-of-array/.
can anyone will explain this problem solution step by step
with code.
plz, help me im very confused .
someone please, reply to me please.
please reply to me i have asked this doubt 15-20 mins ago and no one is replying
wait i am reading the problem now
sorry your doubt was missed by me
in this question you have to use the logic of merging two sorted arrays
and while merging make a count also
now if total no of elements are even then
x= (m+n)/2;
y=x-1;
x and y are index
median is avg of xth element and yth element of merged array
now if total no of elements are odd then
median is (m+n)/2 th element of merged array
first try to solve this question using this hint
meanwhile i will provide you the code as well
the time complexity of previous approach is O(n+m)
which will give TLE
Efficient Approach:
The idea is simple, calculate the median of both the arrays and discard one half of each array.
Letβs take an example to understand this
Input :
arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
brr[] = { 11, 12, 13, 14, 15, 16, 17, 18, 19 }
Recursive call 1:
smaller array[] = 1 2 3 4 5 6 7 8 9 10, mid = 5
larger array[] = 11 12 13 14 15 16 17 18 19 , mid = 15
5 < 15
Discard first half of the first array and second half of the second array
Recursive call 2:
smaller array[] = 11 12 13 14 15, mid = 13
larger array[] = 5 6 7 8 9 10, mid = 7
7 < 13
Discard first half of the second array and second half of the first array
Recursive call 3:
smaller array[] = 11 12 13 , mid = 12
larger array[] = 7 8 9 10 , mid = 8
8 < 12
Discard first half of the second array and second half of the first array
Recursive call 4:
smaller array[] = 11 12
larger array[] = 8 9 10
Now, there are some basic corner cases(Base Case)