algorithm - Given numbers from 1 to 2^32-1, one is missing. How to find the missing number optimally? -
You are given 2 ^ 32-2 unique numbers which are 1 to 2 ^ 32-1, to all numbers It is impossible to fit in memory (thus sorting is not an option). You are asked to find the missing number. What would be the best way to do this problem?
Assume that you can not use a large integer and are limited to 32bit int.
int
Major Editing: Trust me
XOR all of them
I here assume that numbers 1 to 2 32 - 1 inclusive should use 1 additional memory space of 32 bits.
Edit: I thought I might be away with magic Ah well.
Explanation:
Those who know how the hamming codes work, this is the same idea.
Actually, for all the numbers from 0 to 2 n - 1, in each bit position there are exactly 2 (n - 1) 1s . Therefore, achieving those numbers will actually be given 0. However, a number is unavailable, so that particular column will be given one, because in that bit position there is a strange number of people
Although I personally use exponentiation ** I like the operator, I have reverted mine ^ because OP has used it. Do not confuse the X-ray.
Comments
Post a Comment