This post is completed by 2 users
|
Add to List |
4. 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
Output:
Missing number is :6