Asked • 03/19/19

Median of 5 sorted arrays?

I am trying to find the solution for median of 5 sorted arrays. This was an interview questions. The solution I could think of was merge the 5 arrays and then find the median [O(l+m+n+o+p)]. I know that for 2 sorted arrays of same size we can do it in log(2n). [by comparing the median of both arrays and then throwing out 1 half of each array and repeating the process]. .. Finding median can be constant time in sorted arrays .. so I think this is not log(n) ? .. what is the time complexity for this ? 1] Is there a similar solution for 5 arrays . What if the arrays are of same size , is there a better solution then ? 2] I assume since this was asked for 5, there would be some solution for N sorted arrays ? Thanks for any pointers. Some clarification/questions I asked back to the interviewer: Are the arrays of same length => No I guess there would be an overlap in the values of arrays => Yes As an exercise, I think the logic for 2 arrays doesnt extend . Here is a try: Applying the above logic of 2 arrays to say 3 arrays: [3,7,9] [4,8,15] [2,3,9] ... medians 7,8,3 throw elements [3,7,9] [4,8] [3,9] .. medians 7,6,6 throw elements [3,7] [8] [9] ..medians 5,8,9 ... throw elements [7] [8] [9] .. median = 8 ... This doesnt seem to be correct ? The merge of sorted elements => [2,3,4,7,8,9,15] => expected median = 7

2 Answers By Expert Tutors

By:

Patrick B. answered • 07/13/19

Tutor
4.7 (31)

Math and computer tutor/teacher

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.