Home > Algorithms > Given an array of 999 distinct integers ranging from 1 to 1000 including. Find which number is missing

Given an array of 999 distinct integers ranging from 1 to 1000 including. Find which number is missing

December 16, 2011

Problem : Given an array of 999 distinct integers ranging from 1 to 1000 including. Find which number is missing. Restrictions: loop over the array only once, can’t allocate an additional array.

Solution :

Find sum of numbers from 1 to 1000 = 1000*1001/2 =500500
Add the current array sum of 999 numbers = S
The missing number = 500500-S
 

 

Advertisements
Categories: Algorithms
%d bloggers like this: