Computer Programming
Math and Arithmetic
Statistics
Algebra

The integers from 1 to n are stored in an array in random fashion. One of the numbers is missing. What is an example of a program that will find the missing number?

567

Answer

User Avatar
Wiki User
05/18/2008

1. Determine the sum of 1..n = (n(n+1))/2 2. Subtract all the elements in the given array (n-1 elements) from the expected sum. 3. The missing number is the value remaining. /* Implementation in "C" */

int MissingNumber(int values[], int n)

{

int total = (n * (n+1))/2;

while (n>1)

{

total -= values[n-2];

n--;

}

return total;

}