Write a Program to find the deleted number from an array

Leave a Comment

Program to find the deleted number from an array


Suppose you have an array of number 1-50 in random order. The number in an array is not repeated. One number is deleted from an array find the deleted number.


Logic of a Program


1. The number is not repeated so we can calculate the sum of first 50 number. The sum of number  is n(n+1)/2 = 50*(50+1)/2


2. Now one number is deleted from an array, calculate the sum of remaining number using single pass

int sum =0;

for(int i=0;i<49;i++){

      sum = sum + arr[i];

}


Now subtract this sum to the original sum. You will find the missing number.


Hope this is useful. If you know any other solution please comment below.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment