The computer is storing 1024 (distinct) numbers.
You can choose any 2, and it will tell you which number is larger and which is smaller.
What is the minimum number of comparisons that you need in order to find the maximum and minimum of these 1024 numbers?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
I got this same problem (generalized for n numbers) in my algorithms class. Here's my solution submitted for that problem. Suppose that the numbers are integers, just so that I won't write the awkward "number of numbers".
We claim that we need 2 n − 2 − ⌊ 2 n ⌋ comparisons.
For each integer, we give it two flags: possibleMax and possibleMin , indicating that the number is still possible to be the maximum and minimum respectively. On a comparison, we remove the possibleMin flag from the greater integer and the possibleMax flag from the lesser integer, if they exist, as the greater integer can no longer be the minimum and the lesser integer can no longer be the maximum. Our objective is to get only one possibleMax flag and one possibleMin flag.
Consider two integers. If they both have both flags present, then a comparison knocks off two flags. Call such comparison good . However, if at least one flag is missing, in the worst case we might only be able to remove one flag. Clearly any comparison cannot remove more than two flags. Thus the number of comparisons needed is the number of flags, subtracted by two (the actual maximum integer retains its possibleMax flag, and the minimum retains its possibleMin ), subtracted by the number of good comparisons. The number of flags is fixed, 2 n , but the number of good comparisons can vary, denote it as k .
Call an integer that has both flags present pure . Note that every good comparison requires two pure integers; if either integer involved is not pure, then it's possible that the comparison only removes one flag, thus making it not a good comparison. Also note that every good comparison reduces the number of pure integers by two, and there is no way to get an integer from not pure to being pure again. Thus the number of good comparisons is capped above by ⌊ 2 n ⌋ ; there are n pure integers, and a good comparison takes 2 .
Thus, as k ≤ ⌊ 2 n ⌋ , we need 2 n − 2 − k ≥ 2 n − 2 − ⌊ 2 n ⌋ comparisons to complete our objective. This can be achieved: pair off all the integers, dividing them into "greater" and "lesser" set depending on the outcome of their comparisons. This probably leaves one element unpaired; include it in both sets. Then simply find the maximum among the greater integers and the minimum among the lesser integers. The first pairing takes ⌊ 2 n ⌋ comparisons; finding maximum of greater integers takes ⌊ 2 n + 1 ⌋ − 1 comparisons (there are ⌊ 2 n + 1 ⌋ integers, and each comparison removes one candidate at best); finding minimum of lesser integers also takes ⌊ 2 n + 1 ⌋ − 1 comparisons (consider the negations of the integers and find its maximum), totaling the given 2 n − 2 − ⌊ 2 n ⌋ comparisons. Thus our bound is the best possible.
Substituting n = 1 0 2 4 , this gives 2 ⋅ 1 0 2 4 − 2 − ⌊ 2 1 0 2 4 ⌋ = 2 0 4 6 − 5 1 2 = 1 5 3 4 comparisons.