Given two arrays: arr1[0..m-1] and arr2[0..n-1]. Find whether arr2[] is a subset of arr1[] or not. Both the arrays are not in sorted order. It may be assumed that elements in both array are distinct.
Examples:
Input: arr1[] = {11, 1, 13, 21, 3, 7}, arr2[] = {11, 3, 7, 1}
Output: arr2[] is a subset of arr1[]
Input: arr1[] = {1, 2, 3, 4, 5, 6}, arr2[] = {1, 2, 4}
Output: arr2[] is a subset of arr1[]
Input: arr1[] = {10, 5, 2, 23, 19}, arr2[] = {19, 5, 3}
Output: arr2[] is not a subset of arr1[]
#include<stdio.h>
int issubarray(int a[],int b[],int m,int n)
{
int i,j;
int flag;
for(i=0;i<n;i++)
{
flag=0;
for(j=0;j<m;j++)
{
if(b[i]==a[j])
{
flag=1;
}
}
if(flag!=1)
break;
}
if(flag==1)
return 1;
else
return 0;
}
void main()
{
int i,j,m,n;
scanf("%d",&m);
int a[m];
for(i=0;i<m;i++)
scanf("%d",&a[i]);
scanf("%d",&n);
int b[n];
for(j=0;j<n;j++)
scanf("%d",&b[j]);
int x=issubarray(a,b,m,n);
if(x)
printf("Array 2 is a subset of Array1");
else
printf("Array 2 is not a subset of Array1");
}
Examples:
Input: arr1[] = {11, 1, 13, 21, 3, 7}, arr2[] = {11, 3, 7, 1}
Output: arr2[] is a subset of arr1[]
Input: arr1[] = {1, 2, 3, 4, 5, 6}, arr2[] = {1, 2, 4}
Output: arr2[] is a subset of arr1[]
Input: arr1[] = {10, 5, 2, 23, 19}, arr2[] = {19, 5, 3}
Output: arr2[] is not a subset of arr1[]
#include<stdio.h>
int issubarray(int a[],int b[],int m,int n)
{
int i,j;
int flag;
for(i=0;i<n;i++)
{
flag=0;
for(j=0;j<m;j++)
{
if(b[i]==a[j])
{
flag=1;
}
}
if(flag!=1)
break;
}
if(flag==1)
return 1;
else
return 0;
}
void main()
{
int i,j,m,n;
scanf("%d",&m);
int a[m];
for(i=0;i<m;i++)
scanf("%d",&a[i]);
scanf("%d",&n);
int b[n];
for(j=0;j<n;j++)
scanf("%d",&b[j]);
int x=issubarray(a,b,m,n);
if(x)
printf("Array 2 is a subset of Array1");
else
printf("Array 2 is not a subset of Array1");
}
No comments:
Post a Comment