Missing Number

Posted on February 14, 2012

Apart from a few programming puzzles, I never really got to work extensively on 'Bit Array' operations. So, as was practicing a bit, I got an idea for a problem, for which I posted a solution already ( Software Job - Interview Question). This problem is about finding the missing number, in a array of unsorted numbers.

For Ex: We have numbers from 1 to 52 that are put into a 51 number array, what's the best way to find out which number is missing?

As per the idea in that earlier post, the code would look like:

An other solution is to use Java's BitSet, as in the below listed code. Also, note that the following logic is also good for finding multiple missing numbers. So, the below code is a better approach to find one or more than one missing numbers:

Blog Categories
The views expressed on this blog are my personal views and do not reflect the views of my employer or campaigns I am supporting.

All sample code is provided for illustrative purposes only. These examples have not been thoroughly tested under all conditions. The writer therefore, cannot guarantee or imply reliability, serviceability, or function of these programs.

All programs contained herein are provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.