Median of two sorted arrays with different sizes in O ( log ( min ( n , m ) ) ) | Java code for finding median of the given two sorted arrays that exists in the merged array ( ( n + m ) / 2 - 1 position ) ; Function to find median of given two sorted arrays ; If i = n , it means that Elements from a [ ] in the second half is an empty set . If j = 0 , it means that Elements from b [ ] in the first half is an empty set . so it is necessary to check that , because we compare elements from these two groups . searching on right ; If i = 0 , it means that Elements from a [ ] in the first half is an empty set and if j = m , it means that Elements from b [ ] in the second half is an empty set . so it is necessary to check that , because we compare elements from these two groups . searching on left ; We have found the desired halves . ; This condition happens when we don 't have any   elements in the first   half from a[] so we   returning the last   element in b[] from  the first half. ; And this condition happens when we don 't have any   elements in the first half   from b[] so we returning the  last element in a[] from the  first half. ; Function to find maximum ; Driver code ; We need to define the smaller array as the first parameter to make sure that the time complexity will be O ( log ( min ( n , m ) ) )
import java . io . * ; class GFG { static int findMedianSortedArrays ( int [ ] a , int n , int [ ] b , int m ) { int min_index = 0 , max_index = n , i , j ; while ( min_index <= max_index ) { i = ( min_index + max_index ) / 2 ; j = ( ( n + m + 1 ) / 2 ) - i ; if ( i < n && j > 0 && b [ j - 1 ] > a [ i ] ) min_index = i + 1 ; else if ( i > 0 && j < m && b [ j ] < a [ i - 1 ] ) max_index = i - 1 ; else { if ( i == 0 ) return b [ j - 1 ] ; if ( j == 0 ) return a [ i - 1 ] ; else return maximum ( a [ i - 1 ] , b [ j - 1 ] ) ; } } System . out . print ( " ERROR ! ! ! ▁ " + "returningNEW_LINE");  return 0 ; } static int maximum ( int a , int b ) { return a > b ? a : b ; } public static void main ( String [ ] args ) { int [ ] a = { 900 } ; int [ ] b = { 10 , 13 , 14 } ; int n = a . length ; int m = b . length ; if ( n < m ) System . out . print ( " The ▁ median ▁ is : ▁ " + findMedianSortedArrays ( a , n , b , m ) ) ; else System . out . print ( " The ▁ median ▁ is : ▁ " + findMedianSortedArrays ( b , m , a , n ) ) ; } }