This is the problem described in Programming pearls
. I can not understand binary search method descrbied by the author. Can any one helps to elaborate? Thanks.
EDIT: I can understand binary search in general. I just can not understand how to apply binary search in this special case. How to decide the missing number is in or not in some range so that we can choose another. English is not my native language, that is one reason I can not understand the author well. So, use plain english please:)
EDIT: Thank you all for your great answer and comments ! The most important lesson I leant from solving this question is Binary search applies not only on sorted array!
-
Which part do you not understand? Can you elaborate?dirkgently– dirkgently2009年10月29日 09:23:28 +00:00Commented Oct 29, 2009 at 9:23
-
3Binary search is the solution for another problem. It's not suitable to find a value in an unsorted range.Nikolai Ruhe– Nikolai Ruhe2009年10月29日 09:23:45 +00:00Commented Oct 29, 2009 at 9:23
-
What you can't understand? Binary search at all or just authors description?Ilya Khaprov– Ilya Khaprov2009年10月29日 09:25:07 +00:00Commented Oct 29, 2009 at 9:25
-
if array unsorted. We can sort an array nlog(n) (well, somtimes we can sort it with O(n)) then do binary search log(n) this is by 2log(n) times bore then the worst case of sequential search.Ilya Khaprov– Ilya Khaprov2009年10月29日 09:29:11 +00:00Commented Oct 29, 2009 at 9:29
-
Your problem description sounds like you have all numbers 0-2^32-1 with the exception of one number that is missing. Assuming that case and you could find the number that's missing by calculating the sum of all numbers that should be there (this is static) and comparing with the sum of the numbers you actually have.sisve– sisve2009年10月29日 09:37:08 +00:00Commented Oct 29, 2009 at 9:37
6 Answers 6
There is some more information on this web site. Quoted from there:
"It is helpful to view this binary search in terms of the twenty bits that represent each integer. In the first pass of the algorithm we read the (at most) one million input integers and write those with a leading zero bit to one tape and those with a leading one bit to another tape. One of those two tapes contains at most 500,000 integers, so we next use that tape as the current input and repeat the probe process, but this time on the second bit. If the original input tape contains N elements, the first pass will read N integers, the second pass at most N/2, the third pass at most N/4, and so on, so the total running time is proportional to N. The missing integer could be found by sorting on tape and then scanning, but that would require time proportional to N log N."
As you can see, this is a variation on the binary search algorithm: divide the problem into two pieces and solve the problem for one of the smaller pieces.
9 Comments
I believe what the author is getting at is that you pick the midpoint of your current range of integers, and prepare two output files. As you read your input, everything above the midpoint goes into one file, and everything below the midpoint goes into the other.
Once that's finished, you pick whichever of the files is smaller, and then repeat the operation, using [lower bound, midpoint] or (midpoint, upper bound] as your new range, until the file and range are small enough to switch to the bitmap pattern (or one of your output files is empty).
Damien
4 Comments
The general idea is this: pick a range of integers, and select all the integers that fall within that range. If the number of integers is less than the size of your range, you know that that range contains one or more missing numbers.
This applies to the original problem of how you know there are some missing numbers in the first place too.
Comments
If you consider numbers in the range from 1 to N; half of them are larger than N/2 and half of them smaller than N/2
The ones larger than N/2 would have the MSB set to one; MSB = 0 for the smaller ones.
Partition the whole set based on MSB which will give you two sets : set of numbers smaller than N/2 and set of number larger than N/2
The partition smaller in size has the missing element.
In the next step, use the next MSB.
If the smaller set was less than N/2, half of them are less than N/4 (2nd MSB=0) and the other half larger than N/4 (2nd MSB=1)
If the smaller set was larger than N/2, half of them are less than N/2+N/4 (2nd MSB=0) and the other half larger than N/2+N/4 (2nd MSB=1)
Each round will halve the search space and that's it.
Sum ( N / 2^i ) for 0 <= i < log N gives you O(N)
Comments
This is basically the same question as this question. The same approach works here for the ample memory case to get O(N) complexity. Basically just recursively try to put every integer to its correct location and see what doesn't have the correct value.
Comments
Well, it's about a file that contains all 4 billion integers except one! That is the catch in this case.
- As you move along the list of integers, compute the sum.
- At the end, compute the sum as if there were all integers present using the formula N * (N + 1) / 2
- Extract the sum at (1) from the sum you computed at (2). That is the missing integer.
Example: Assume we have the following sequence: 9 3 2 8 4 10 6 1 7 (1 through 10 with 5 missing). As we add integers sequentially, we get 9 +たす 3 +たす 2 +たす 8 +たす 4 +たす 10 +たす 6 +たす 1 +たす 7 =わ 50. The sum of all integers from 1 to 10 would be 10 * (10 + 1) / 2 = 55. Therefore, the missing integer is 55 - 50 = 5. Q.E.D.
8 Comments
Explore related questions
See similar questions with these tags.