Given an array, A, containing, in some order, all but one of the k-bit non-negative integers find the missing integer with the following constraint:
You are allowed to do only two things with one of these integers:
- Obtain the value of its ith bit
- Swap them with one another in the array
For example: if k is 3 the array will contain all but one the integers 0 through 7 inclusive, the task is to find the missing integer.
You should check that each bit (for example, if k = 3 you need to check that all 3 bits) are present 2^k-1 times as 0 & 1.
If you are sure of the invariant of the list that you outlined (only one number is missing and there are no duplicates) – sure that it always holds – then you could simply check only one, arbitrary bit holds the condition outlined above.
For example, say that the invariant holds, k = 3, and you have the list [0-7], excluding 6.
For each number in the list, get its first bit (least significant) and do the following:
if bit value = 0 then zeroValues++;if bit value = 1 then oneValues++;
zeroValuesshould equaloneValuesand both should equal2^k-1, in this case 4.Edit: Rereading your question, you’re looking for the entire number. In order to find the whole integer value that you are looking for, just do the procedure for all bits. For each bit you do it, you will find either a 0 value or 1 value missing. The missing value is the bit value in the result integer. Doing this for all bits will find the entire bit representation of your integer.