Re: Algorithm to find break in contiguity
- From: rem642b@xxxxxxxxx (robert maas, see http://tinyurl.com/uh3t)
- Date: Mon, 29 Jan 2007 08:09:47 -0800
From: use...@xxxxxxxxxxxxxxx
I need an efficient algorithm for the following problem:
If you have unique N-1 numbers in the range 1 to N, find the
missing number.
What's best depends almost entirely on how your original data is
given to you. If it's stored in a disk file, you probably can't
save any significant time by jumping around doing a binary search,
best to just read the whole file sequentially, but in that case you
might as well compare each pair of adjacent numbers (or checking
each number individually against what's expected at that spot if
the missing number hasn't been reached yet) as you are reading.
With buffered I/O, you will probably spend most of your time
finished checking the current block and waiting for the next block
to load. That's assuming the numbers are stored in ascending
sequence.
If the data is stored sequentially in a array, or some sort of
balanced binary tree, you can do a binary search to find where the
jump over the missing number occurs.
If the data is stored sequentially in a linked-list, the answer
depends on whether it's small enough to be mapped into RAM at the
same time, in which case sequential scan down the links is optimal,
or whether it's so huge that sequential scan down the links would
thrash virtual memory, in which case it's a much more difficult
problem to solve.
If the data is not already in sequence, that's a whole 'nother problem.
You didn't state the problem clearly enough to know where you're
getting your data from in what sequence in what form.
Since you used the word "contiguity" in the Subject field, I
*assume* the data is in ascending or descending sequence in either
contiguous memory (disk file or array) or at least logically
contiguous (linked list or binary tree), but that might not have
been correct.
.
- Follow-Ups:
- Re: Algorithm to find break in contiguity
- From: Richard Heathfield
- Re: Algorithm to find break in contiguity
- Prev by Date: Re: Algorithm to find break in contiguity
- Next by Date: Re: looking for name for data structure
- Previous by thread: Re: Algorithm to find break in contiguity
- Next by thread: Re: Algorithm to find break in contiguity
- Index(es):
Relevant Pages
|