Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is a classic question - I was actually just asked it a few weeks ago. To solve it, you take the number of possible distinct numbers that could appear, and allocate that many bits. </p> <p>For example, if numbers in the list must be between 1-20, you allocate 20 bits - one for each number, and you initialize each bit as 0.</p> <p>Then you traverse the list. Every time you see a number, flip the corresponding bit. </p> <p>For example: With your example list of 2 6 3 6 5 7 7, we could allocate 7 bits (for 1 2 3 4 5 6 7). Then as we traverse the list, we would do the following:</p> <ul> <li>flip 2nd bit</li> <li>flip 6th bit</li> <li>flip 3rd bit</li> <li>flip 6th bit</li> <li>etc</li> </ul> <p>Then once you're done traversing the list, you can read through the bits to find the three unique numbers. They will all be represented by '1' bits, and the other numbers will be represented by 0s.</p> <p>You read through the list twice, which takes 2n time, which is O(n).</p> <hr> <p>Edit: It is possible that the bounds will not be given. One solution, then, is to simply read through the list first to determine the bounds yourself - then it's still O(n).</p> <p>However one problem which could occur is that the list could be very small, but some very large numbers - effectively making the range too big. For example:</p> <pre><code>1, 99999999999999999, 1, 99999999999999999, 2, 3, 4 </code></pre> <p>Solving that problem would require a lot of memory due to the large number present in the list, because even though there are very few numbers, the range is very large and we are allocating bits according to the range.</p> <p>The solution could then be adjusted to give a new solution as follows using a hashtable (although I'm not sure if this is permitted given the problem's "bit manipulation only" stipulation):</p> <ol> <li>Let <code>L</code> denote the original list, and <code>C</code> denote a copy of it.</li> <li>Remove all duplicates from <code>C</code> (there are numerous ways of doing this efficiently).</li> <li>Create a hashtable <code>H</code>, and for each element in <code>C</code>, insert a key/value pair &lt;<code>number</code>,<code>pos</code>> into <code>H</code> where <code>number</code> is the current element in <code>C</code>, and <code>pos</code> is its position in <code>C</code>. So, given a number that appears in <code>L</code>, we can now use <code>H</code> to find that number's position in <code>C</code>.</li> <li>Allocate a number of bits equal to the size of <code>C</code>, and initialize those bits to 0.</li> <li>Traverse <code>L</code>. Each time we run accross a number, get its value from <code>H</code>, and flip that bit in our bit list.</li> <li>Traverse the bit list - for each '1' bit, get the number from <code>C</code> which is at that position - that is one of the unique numbers.</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload