in reply to first missing number

I'm not sure this saves any time. I didn't follow the algorithm, but if I understand the problem correctly, you still need to check $array[0] through $array[$n] regardless, if you're going to declare $n+1 as a first missing number. So why probe anything beyond that?

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: Re: first missing number
by chipmunk (Parson) on Dec 19, 2000 at 11:27 UTC
    I think the problem description should be made a little more specific; the array must contain only positive integers, and must have no duplicates. With that in mind, you don't have to check $array[0] through $array[$n]. If $array[$n] == $n+1 -- e.g., if $array[25] is 26 -- then you know that there are no missing numbers in @array[0..$n].
      Ahh... the word "sorted" was missing when I first looked at it. OK, then yes, you can determine it with O(log n) probes. Silly me. {grin}

      -- Randal L. Schwartz, Perl hacker