Re: searching for missing element in an array



pete <pfiland@xxxxxxxxxxxxxx> writes:

srk wrote:
Suppose we have n numbers(from 1 to n) and there is an array of size
n-1. How can we find, which number is missing from the array if
numbers 1 to n are being placed in array randomly.

There are other ways to do it but,
your question is an xor checksum exercise.

I think that it takes a programmer to regard this as a question
most naturally answered with xor. Anyone else would use
subtraction: sum the numbers in the array and subtract from
n*(n+1)/2.
--
Ben Pfaff
http://benpfaff.org
.



Relevant Pages