Write a C program to find the missing integer among the array of integers that are in the range 1 to n.
Input Format:
The first line of the input consists of an integer, n that corresponds to the number of elements in the input array.
The next 'n' lines in the input correspond to the elements in the array.
Output Format:
Output is an integer.
Refer sample input and output for formatting specifications.
Sample Input1:
5
1
2
4
5
6
Sample Output1:
3
Sample Input2:
6
1
2
3
4
7
6
Sample Output2:
5
Program Code:
#include<stdio.h>
int getMissingNo (int a[], int n)
{
int i, total;
total = (n+1)*(n+2)/2;
for ( i = 0; i< n; i++)
total -= a[i];
return total;
}
int main()
{
int a[100], i, n;
scanf("%d", &n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int miss = getMissingNo(a,n);
printf("%d", miss);
return 0;
}
Post A Comment:
0 comments: