algorithm - Removing even/odd numbers to find missing number in an array of array of numbers represented in binary -
this related problem 5.7 (under bit manipulation) in 5th edition of cracking coding interview (if question not appropriate so, please let me know correct site , i'll move it):
an array a[1..n] contains integers 0 n except 1 number missing. in problem, cannot access entire integer in single opera-tion. elements of represented in binary, , operation can use access them “fetch jth bit of a[i]”, takes constant time. write code find missing integer. can in o(n) time?
the algorithm applied this:
- check lsbs of numbers in list.
- count occurrence of 1's , 0's in lsbs. if count(0)<=count(1), lsb of missing number 0. else 1.
- remove numbers lsb not matching result found in step 2.
- repeat 1 4, , progressively check next lsb in each iteration.
can explain logic behind step 3? removes odd/even numbers current list (depending on bit found missing number) , uses modified list in next iteration. why this?
step 3 meant (vastly) improve runtime of algorithm. if step 3 included, overall algorithm binary search algorithm using lsb branching decider. if step 3 omitted, still binary search, 1 implemented in way not become logarithmically faster on each pass (which exceed o(n) bound).
incidentally, written seems there's bit shift missing, or term lsb being used in rather liberal way.
Comments
Post a Comment