Friday 26 September 2014

Missing Integer Problem

You are given a sequence of n-1 distinct positive integers, all of which are less than or equal to a integer ‘n’. You have to find the integer that is missing from the range [1,2,...,n]. Solve the question without using arrays.

Input Format:
One line containing the integer ‘n’ where 2<=n<=10,000
First line is followed by a sequence of ‘n-1’ distinct positive integers. Note that the sequence may not be in any particular order.

Output Format: 
One line containing the missing number.


#include<stdio.h>

int main()
{int n,i,b;
scanf("%d",&n);

int a[10000]={0};

for(i=1;i<=n;i++)
{
scanf("%d",&b);
a[b]=a[b]+1;}

for(i=1;i<=n;i++)
{
if(a[i]==0)
{
printf("%d",i);
}}return 0;
}



No comments:

Post a Comment