Skip to main content
deleted 43 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Just a few thoughts on how you might improve your algorithm as stated:

  1. 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]==0array[0] == 0) thenthen if array[n] != narray[n] != n you have 'missing' 'missing' values.
  2. 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:

  1. Do no work if there are no missing values.
  2. Stop work as soon as it finds the requisite number of 'missings'.

Hope that is helpful?

Just a few thoughts on how you might improve your algorithm as stated:

  1. The question doesn't state that there will Always be a missing number. So might it be good (given that the first value is guaranteed to be 0 so array[0]==0) then if array[n] != n you have 'missing' values.
  2. If we accept that we have missing values in that case, then the difference between n and the value stored in array[n] tells us how many 'missings' we have.

Those two bits of information would allow us to truncate your algorithm to:

  1. Do no work if there are no missing values
  2. Stop work as soon as it finds the requisite number of 'missings'.

Hope that is helpful?

Just a few thoughts on how you might improve your algorithm as stated:

  1. The question doesn't state that there will always be a missing number. So might it be good (given that the first value is guaranteed to be 0 so array[0] == 0) then if array[n] != n you have 'missing' values.
  2. If we accept that we have missing values in that case, then the difference between n and the value stored in array[n] tells us how many 'missings' we have.

Those two bits of information would allow us to truncate your algorithm to:

  1. Do no work if there are no missing values.
  2. Stop work as soon as it finds the requisite number of 'missings'.
Source Link

Just a few thoughts on how you might improve your algorithm as stated:

  1. The question doesn't state that there will Always be a missing number. So might it be good (given that the first value is guaranteed to be 0 so array[0]==0) then if array[n] != n you have 'missing' values.
  2. If we accept that we have missing values in that case, then the difference between n and the value stored in array[n] tells us how many 'missings' we have.

Those two bits of information would allow us to truncate your algorithm to:

  1. Do no work if there are no missing values
  2. Stop work as soon as it finds the requisite number of 'missings'.

Hope that is helpful?