Edit 1: This question is a duplicate of https://gateoverflow.in/227458/sorting?fbclid=IwAR1GJNWosv560XE1VFqji7KAwgPoVjLXgZuKF0CLfMPDGDR2J6PsTYYjPcs
Edit 2: For getting the median/middle efficiently we don't have to necessarily perform the union or merge operation. The method 2 shown here https://www.geeksforgeeks.org/median-of-two-sorted-arrays/ works better than finding the union first and finding the median. So it can be done in O(log n ) time.
Why not O(n)?
The question is asking for the middle element of the union of 2 sorted arrays. By "middle" I am assuming that it wants the value at the middle index, please correct me if I am wrong here. I did not see anything in the question that makes me search for the median as others have suggested.
Consider
A[] = {1,3,4,5,7}
B[] = {2,3,5,6,8}
Both the sorted arrays are specified as given. Both have equal length and distinct elements in each.
Now the Union array would be
U[] = {1,2,3,4,5,6,7,8}
Which would take O(n+n) time.
Now, middle of U[] can be found out by
int mid = firstIndex + (lastIndex-firstIndex)/2
which would take O(1) time, and so total time would be O(n).