Just a few thoughts on how you might improve your algorithm as stated:
- The question doesn't state that there will Alwaysalways be a missing
number number. So might it be good (given that the first value is
guaranteed guaranteed to be 0 so array[0]==0
array[0] == 0) thenthen if array[n] != narray[n] != nyou have 'missing' 'missing' values. - If we accept that we have missing values in that case, then the
difference difference between n and the value stored in array[n]
array[n]tells us how many many 'missings' we have.
Those two bits of information would allow us to truncate your algorithm to:
- Do no work if there are no missing values.
- Stop work as soon as it finds the requisite number of 'missings'.
Hope that is helpful?