Find median of 2 sorted arrays if they were merged

Solution: A bianry algorithm for this is as follows:

Compare the medians of the 2 arrays.

If they are equal, median is found.

Else if arr1[mid] < arr2[mid], then:

Eliminate the smaller half of arr1 and

Eliminate the greater half of arr2 as they will form the first quarter and last quarter of the merged arrays.

Recurse for the remaining halves of each array to get the median of the merged arrays.

The above algorithm works well till we get the array sizes down to 2
When any of the arrays' size becomes less than 2, then special cases arise.
Let A[N] and B[M] be the arrays with sizes N and M such that N < M
There are 4 cases:

N=1 and M=even: Median is median of 3 numbers: A[0], B[M/2 - 1] and B[M/2]

N=1 and M=odd: Median is median of 4 numbers: A[0], B[M/2 - 1], B[M/2] and B[M/2 + 1]

N=2 and M=odd: Median is median of 3 numbers: B[M/2], max(A[0], B[M/2 - 1]), min(A[1], B[M/2 + 1])

N=2 and M=even: Median is median of 4 numbers: B[M/2], B[M/2 - 1], max(A[0], B[M/2 - 2]), min(A[1], B[M/2 + 1])

Got a thought to share or found a bug in the code? We'd love to hear from you: