Problem Statement
Write a c program to print the second array integers present in the first array integers.
Solution
#include<stdio.h>
#include <stdlib.h>
int main()
{
long long int n,m,k=0,i,j,count,t;
scanf("%lld%lld",&n,&m);
int a[n],b[m],c[n];
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<m;i++)
{
scanf("%d",&b[i]);
}
for(i=0;i<m;i++)
{
count=0;
for(j=0;j<n;j++)
{
if(b[i]==a[j])
count++;
}
{
while(count!=0)
{
printf("%d ",b[i]);
count--;
}
}
}
for(i=0;i<n;i++)
{
count=0;
for(j=0;j<m;j++)
{
if(a[i]==b[j])
count++;
}
if(count==0)
c[k++]=a[i];
}
for(i=0;i<k;i++)
{
for(j=i+1;j<k;j++)
{
if(c[i]>c[j])
{
t=c[i];
c[i]=c[j];
c[j]=t;
}
}
}
for(i=0;i<k;i++)
{
printf("%d ",c[i]);
}
return 0;
}
Note:
we have to firstly print the second array elements based on frequency compared
with the first array elements then print the remaining first array elements in sorting way.
Input 1
9 3
10 7 1 4 5 4 7 5 4
7 4 5
output 1
7 7 4 4 4 5 5 1 10
Input 2
7 2
2 1 3 2 0 2 4
0 1
Output 2
0 1 2 2 2 3 4