1

Two sorted arrays of integers A[1..N] and B[1..N] are provided in ascending order.

Q:Design an O(log N)-time algorithm for finding out the median of all 2N integers. N may not power of 2.

To make thing easy, we can assume O(1) algorithm which return m such that:

2^m < N <  2^m+1

My problems:

  1. N may not be power of 2, what does that mean? (understood)
  2. I don't know how to change the input and make the length to power of 2 after found min and max elements from both array A and B.
James
  • 37
  • 7
  • 1
    x to the power of y is basic maths - see http://www.bbc.co.uk/bitesize/ks3/maths/number/powers_roots/revision/2/ - 11 year old kids in the UK learn this stuff – Ed Heal Aug 15 '17 at 10:53
  • `$(2)^(m)$ < N < $(2)^(m+1)$` - What does this mean? – Ed Heal Aug 15 '17 at 10:56
  • This of course i know. N may not be power of 2 is it means that total number of elements in the array may be N power of 1,3,4,5,6,...,m? – James Aug 15 '17 at 10:57
  • It means N can not be in the group 1,2,4,8,16,32,64,128,256,1024,2056,4096,.... – Ed Heal Aug 15 '17 at 10:59
  • i.e. N can be any number – Ed Heal Aug 15 '17 at 11:00
  • @EdHeal "N (can be) (not in the group ... )" => "You can't assume that N is in the group ..." => "N can be any number" – Caleth Aug 15 '17 at 14:42
  • @User1234 the "power of 2" stuff is all to tell you that dividing arrays into halves doesn't guarantee those halves have the same size (e.g. the first may have 1 more element than the second). This also relates to the definition of a median, which is subtly different for odd and even sized collections – Caleth Aug 15 '17 at 14:45
  • I get the point. But min and max have what use in order to change the length to power of 2? – James Aug 16 '17 at 15:47
  • @Caleth do u know how? – James Aug 17 '17 at 03:42
  • I don't think you have to *change* anything here. These notes are hints about how this is graded – Caleth Aug 17 '17 at 07:43

1 Answers1

4

You can solve this in O(logN) time using a binary search style approach. Consider the following two arrays:

1 1 2 2 3
1 2 3 4 5

Now the combined median is:

1 1 1 2 2 2 3 3 4 5 => 2

Let's see how we can find it. Start by guessing that the median is the center of each array:

1 1 2 2 3 => 2
1 2 3 4 5 => 3

Logically, we know the combined median can't possibly be less than 2 or greater than 3. Rather, it must be somewhere in between these two values. So we can discard everything in the first array smaller than 2 and everything in the second array larger than 3. This won't affect the position of the median because we are discarding an equal number of elements on both sides of where the combined median is. Conceptually, this leaves us with:

2 2 3 => 2
1 2 3 => 2

Now we already have an agreeing median, but the basic idea is to keep discarding half of the entries in each of the two arrays until we have a single median value.

This algorithm will perform as well as binary search, which is O(logN).

Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263
  • This i can understood. But how can i design in code for the second problem that i have stated and 2^m < N < 2^m+1? – James Aug 15 '17 at 11:14
  • I also don't understand this second part, maybe your prof has bad hand writing or was drunk or something. If you want codr for this, just Google binary search, this will already get you more than halfway there. – Tim Biegeleisen Aug 15 '17 at 11:19
  • @User1234 Your prof is saying that `floor(log2(x))` is a constant time operation – Caleth Aug 15 '17 at 14:49
  • Now i stuck at change the input and make the length to power of 2 after found min and max elements from both array A and B.(I have found the min and max of both array since both array are sorted in ascending order so it is very obvious)In the form of code – James Aug 15 '17 at 15:05
  • and the combined median should be redundant rite since we just nid to find median and cut both array into half then take the upper part of A and lower part of B. – James Aug 16 '17 at 02:41