Google Add

Search

Write a Program to Find Missing Number From an Array

Suppose you have given an array of 1 to 100 numbers. The array is sorted. One number is missing from an array. You have to find the missing number.

This question is little tricky and one of the most asked questions in an interview. To solve this problem here we use mathematical trick.

Logic

1. Array is sorted. So first we calculate the sum of 100 numbers.
                  total_sum = n*(n+1)/2;

2.  Add the given numbers in an array.

        for(i=0;i<n;i++){

               sum +=arr[i];

          }



3.    Subtract the sum with the total sum of 100 numbers you will get the missing number.
        missing_num = total_sum - sum;


More Interview Questions


No comments:

Post a Comment