Sunday, February 25, 2018

C program : Given two arrays of integers output the smallest number in the first array not present in the second one.

Input Specification: 
The first line contains the size N1 of the first array.
Next line give the contents of the first array.
Next line contains the size N2 of the second array.
Next line give the contents of the second array. 
 
Output Format:
Output must be a single number which is the smallest number occurring
in the first array that does not occur in the second. In case there is
no such number, output NO.
 
 #include<stdio.h>
# define MAX_SIZE 20

int main(){
int a[MAX_SIZE], b[MAX_SIZE],comn[MAX_SIZE];
int na,nb,count=0,g=0;
scanf("%d",&na);
if(na>0)
{
for(int i=0;i<na;i++)
{

    scanf("%d ",&a[i]);

}
scanf("%d",&nb);
for(int j=0;j<nb;j++)
{

    scanf("%d ",&b[j]);

}

for(int k=0;k<na;k++){

    for(int l=0;l<nb;l++)
    {
        if(a[k]==b[l])
        {
            count++;
        }


    }
    if(count==0)
    {
        comn[g]=a[k];
        g+=1;
    }
    if(count>=1)
    {
        ;
    }
    count=0;
}
int smallest=comn[0];
for(int j=0;j<g;j++)
{
    if (comn[j]<smallest)
    {
        smallest = comn[j];

    }

}
if(g!=0)
{
    printf("%d", smallest);
}
else
{
    printf("NO");
}
}
else
{
    printf("NO");
}

return 0;
}
 
Thanks
Happy Computing ! 
 

1 comment:

How can I run a C++ program directly from Windows?

How-can-I-run-a-C-program-directly-from-Windows