Re: searching for missing element in an array



On Thu, 29 May 2008 12:39:08 +0100, rossum wrote:

On Thu, 29 May 2008 03:10:32 -0700 (PDT), srk <kumarsr@xxxxxxxxx> wrote:

Hi,
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.

Thanks
SRK
Total all the numbers in the array.

The numbers from 1 to n add up to (n * (n + 1)) / 2.

The difference is the missing number.

rossum

An other option would be to *multiply* all the entries, and divide n! by
the sum ;-)

AvK
.



Relevant Pages

  • Re: Finding Repeated and Missing Numbers
    ... That said, there is missing information. ... If array one has ... to hold the result without overflow (since as you noted the sum is ... Reading email is like searching for food in the garbage, ...
    (comp.lang.c)
  • Re: Finding Repeated and Missing Numbers
    ... missing = sum - Sum; ... How would you find the only missing element in that array? ... > think of a way to do it while iterating through the array only once. ... can't overflow. ...
    (comp.lang.c)
  • Re: searching for missing element in an array
    ... What happens if the array was initialized to all -1 values? ... we know that a zero is stored in the slot of the missing number? ... initialization of the array. ... that there are n-1 numbers present. ...
    (comp.programming)
  • Re: searching for missing element in an array
    ... What happens if the array was initialized to all -1 values? ... we know that a zero is stored in the slot of the missing number? ... initialization of the array. ... there are n-1 numbers present. ...
    (comp.programming)
  • Re: how to find missing value.
    ... then missing number,n = Sum of N natural numbers + Sum of the numbers in Array A. ... the set of numbers would be consecutive if it were sorted, with the exception of the missing number. ...
    (perl.beginners)