Easy interview question got harder: given numbers 1..100, find the missing number(s) given exactly k are missing

前端 未结 30 1315
时光说笑
时光说笑 2020-11-22 07:02

I had an interesting job interview experience a while back. The question started really easy:

Q1: We have a bag containing numbers

30条回答
  •  悲&欢浪女
    2020-11-22 07:45

    Here's a solution that uses k bits of extra storage, without any clever tricks and just straightforward. Execution time O (n), extra space O (k). Just to prove that this can be solved without reading up on the solution first or being a genius:

    void puzzle (int* data, int n, bool* extra, int k)
    {
        // data contains n distinct numbers from 1 to n + k, extra provides
        // space for k extra bits. 
    
        // Rearrange the array so there are (even) even numbers at the start
        // and (odd) odd numbers at the end.
        int even = 0, odd = 0;
        while (even + odd < n)
        {
            if (data [even] % 2 == 0) ++even;
            else if (data [n - 1 - odd] % 2 == 1) ++odd;
            else { int tmp = data [even]; data [even] = data [n - 1 - odd]; 
                   data [n - 1 - odd] = tmp; ++even; ++odd; }
        }
    
        // Erase the lowest bits of all numbers and set the extra bits to 0.
        for (int i = even; i < n; ++i) data [i] -= 1;
        for (int i = 0; i < k; ++i) extra [i] = false;
    
        // Set a bit for every number that is present
        for (int i = 0; i < n; ++i)
        {
            int tmp = data [i];
            tmp -= (tmp % 2);
            if (i >= even) ++tmp;
            if (tmp <= n) data [tmp - 1] += 1; else extra [tmp - n - 1] = true;
        }
    
        // Print out the missing ones
        for (int i = 1; i <= n; ++i)
            if (data [i - 1] % 2 == 0) printf ("Number %d is missing\n", i);
        for (int i = n + 1; i <= n + k; ++i)
            if (! extra [i - n - 1]) printf ("Number %d is missing\n", i);
    
        // Restore the lowest bits again.
        for (int i = 0; i < n; ++i) {
            if (i < even) { if (data [i] % 2 != 0) data [i] -= 1; }
            else { if (data [i] % 2 == 0) data [i] += 1; }
        }
    }
    

提交回复
热议问题