Set Union
Limits
1s, 512 MB
Given two sets of integers, print the set containing the union of the two input sets.
For example, given the sets:
For example, given the sets:
- {1, 3, 5, 7, 8}
- {2, 3, 4, 7, 9}
- {1, 2, 3, 4, 5, 7, 8, 9}
Input
The first line of the input will contain two integers N and M (0 < N, M < 100).
The following line will contain N integers, the members of the first set.
The line after that will contain M integers, the members of the second set.
The members of both sets will be given in increasing order.
The following line will contain N integers, the members of the first set.
The line after that will contain M integers, the members of the second set.
The members of both sets will be given in increasing order.
Output
Print the members of the union set in the same line, separated by space sorted in increasing order.
#include<stdio.h>
int main()
{
int n1,n2,len,i,j=0,k=0,temp;
scanf("%d%d", &n1, &n2);
len=n1+n2;
int a[n1],b[n2],c[len];
for(i=0; i<n1; i++)
{
scanf("%d", &a[i]);
}
for(i=0; i<n2; i++)
{
scanf("%d", &b[i]);
}
for(i=0; i<n1; i++)
{
c[j++]=a[i];
}
for(i=0; i<n2; i++)
{
c[j++]=b[i];
}
for(i=0;i<len;i++)
{
for(j=i+1;j<len;j++)
{
if(c[i]>c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
for(i=0; i<len; i++)
{
for(j=i+1;j<len;)
{
if(c[j]==c[i])
{
for(k=j;k<len;k++)
{
c[k]=c[k+1];
}
len--;
}
else
{
j++;
}
}
}
for(i=0; i<len-1; i++)
printf("%d ", c[i]);
printf("%d", c[len-1]);
}