Python program to find k - th element from two sorted arrays
def kth ( arr1 , arr2 , n , m , k ) : NEW_LINE INDENT if n == 1 or m == 1 : NEW_LINE INDENT if m == 1 : NEW_LINE INDENT arr2 , arr1 = arr1 , arr2 NEW_LINE m = n NEW_LINE DEDENT if k == 1 : NEW_LINE INDENT return min ( arr1 [ 0 ] , arr2 [ 0 ] ) NEW_LINE DEDENT elif k == m + 1 : NEW_LINE INDENT return max ( arr1 [ 0 ] , arr2 [ 0 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT if arr2 [ k - 1 ] < arr1 [ 0 ] : NEW_LINE INDENT return arr2 [ k - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT return max ( arr1 [ 0 ] , arr2 [ k - 2 ] ) NEW_LINE DEDENT DEDENT DEDENT mid1 = ( n - 1 ) // 2 NEW_LINE mid2 = ( m - 1 ) // 2 NEW_LINE if mid1 + mid2 + 1 < k : NEW_LINE INDENT if arr1 [ mid1 ] < arr2 [ mid2 ] : NEW_LINE INDENT return kth ( arr1 [ mid1 + 1 : ] , arr2 , n - mid1 - 1 , m , k - mid1 - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return kth ( arr1 , arr2 [ mid2 + 1 : ] , n , m - mid2 - 1 , k - mid2 - 1 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if arr1 [ mid1 ] < arr2 [ mid2 ] : NEW_LINE INDENT return kth ( arr1 , arr2 [ : mid2 + 1 ] , n , mid2 + 1 , k ) NEW_LINE DEDENT else : NEW_LINE INDENT return kth ( arr1 [ : mid1 + 1 ] , arr2 , mid1 + 1 , m , k ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr1 = [ 2 , 3 , 6 , 7 , 9 ] NEW_LINE arr2 = [ 1 , 4 , 8 , 10 ] NEW_LINE k = 5 NEW_LINE print ( kth ( arr1 , arr2 , 5 , 4 , k ) ) NEW_LINE DEDENT