Question

Assume we're given a list of n numbers and we want to find a number that is greater than or equal to the median. I want to learn the lower bound for the worst case complexity of this problem. I know that the lower bound of finding the median is 3(n-1)/2. But will it be the same when we want to find a number that is greater than or equal to the median.

Was it helpful?

Solution

I think the largest element of the first half of the list(+1) will have this feature. If You check n/2+1 element and You store the greatest one, there could be at most n/2-1 element greater than your median-candidate. So, the chosen number will be in the top half of the numbers, which means: it is greater than or equal to the median.

So You can find it in n/2+1 .

You need:

worst case: n/2 comparsions and n/2+1 assignments.

best case: n/2 comparsions and 1 assignment.

Edit: Answer to Your comment:

Yes. If n is an even number, any random element will be greater than or equal to the median with probability at least 0.5. Why "at least 0.5"? There might be test cases, where nearly all of the number are equal to the median. In those cases, the probability will be higher. If You want to know the correct probability, You have to check all the elements. In other test cases with diferent numbers, any random element would be in the top half of the ordered list with probability 0.5.

If n is odd, a random number will have this feature with the probability > 0.5. It's beacause n/2-0.5 elements are less than the median and n/2+0.5 elements are >= median ( in a common test case). If You'd like 0.5 to be the minimal probability, You should do some modification. I have an idea with no proof, maybe somebody will coorect me if it doesn't work: Chose 2 random values from the list. The smaller will be a valid solution with at least 0.5 probability .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top