Find a Missing Number From a Sequence of Consecutive Numbers

Objective: You have been asked to write an algorithm to Find a Missing Number From a Sequence of Consecutive Numbers

Input:  Array, arrA[] with a missing number and Range

Output: missing number

Approach:

  • The approach is very simple, Add all the given numbers say S
  • Calculate the sum of N numbers by formula n(n+1)/2, say N
  • Find missing number m = N-S


Example : suppose array given is  {1,2,3,4,5,6,8,9,10} and range is 10.

So N will be sum of 1 to 10  = 10(10+1)/2 = 55

S will be the sum of all the array elements which is = 48

So missing number will be = 55-48 = 7

Code:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.