find missing number in array. Given an array of randomly field numbers ,but there is one random empty slot in the array find the missing numbers? The numbers are range from 1 to N, where N is  size of the array.

Example:
I/P    [1, 2, ,5 ,4 , 6, 7, 8, 9]
Missing Number is 3

Solution using arithmetic sum formula:

 find missing number in array

 

 

 

 

 

  1. Get sum of all numbers using above formula.
  2. subtract each number from total sum.
  3.  Return remainder of the step 2. we will get missing number.

#include<stdio.h>

// arithmetic sum formula

int sumTotal (int n)
{
return n * (n + 1) / 2;
}

int
main ()
{

// given array
int arr[] = { 1, 2, 5, 4, 6, 7, 8, 9 };
int size = sizeof (arr) / sizeof (arr[0]);
int sum = 0;
int i = 0;

// sum total
sum = sumTotal (9);

// substract each number from arr
for (i = 0; i < size; i++)
sum = sum - arr[i];

printf ("missing number is %d \n", sum);

return 0;
}



Related Contents to follow