Write a C program to check whether a given array is a subset of another given array. We can call an array a subset of another array if all the elements of the array are present in the other array as well.
The set of inputs required are the sizes or number of elements in the two arrays along with the elements or data values of the two array. Our desired expected output is a statement stating whether the given array is a subset of the other array or not.
The first step is always to gather the required set of inputs. To do so, wec can make use of predefined input function called scanf() to read the inputs at runtime.
It is well-known to read any primitive datatype input and on the basis of the format specifier given in the function, the datatype of the input being read is determined.
Since all the inputs of our problem statement are of integer type, we use the ‘%d’ format specifier. First, we read the sizes of the two arrays (n1, n2) and create the array of the respective size (arr1[n1], arr2[n2]). Then, we iterate through both the arrays individually and read the elements of the array.
printf(“\nEnter the number of elements in array 1 : “);
scanf(“%d”, &n1);
printf(“\nEnter the number of elements in array 2 : “);
scanf(“%d”, &n2);
int arr1[n1],arr2[n2];
printf(“\nEnter the %d positive elements in array 1 : “,n1);
for(i=0;i<n1;i++) {
scanf(“%d”,&arr1[i]);
}
printf(“\nEnter the %d positive elements in array 2 : “,n2);
for(i=0;i<n2;i++) {
scanf(“%d”,&arr2[i]);
}
After getting all the inputs, we have to iterate through the second array and check for every element in second array (arr2), if the same element exists in first array (arr1). To do that, for every element in the second array, we iterate through the first array and check if there exists an element equal to it.
If yes then, we change that value to -1 indicating that, this element has already matched one of the elements in the second array and then, increment the count variable which is initially zero, by 1. After doing so, we break the inner loop and check for the next element in the second array.
for (i = 0; i < n2; i++) {
for (j = 0; j < n1; j++) {
if(arr2[i] == arr1[j]) {
count++;
arr1[j]=-1;
break;
}
}
}
This continues until all the elements in the second array are checked. After completion of all the iterations we see whether the count variable equals the size of second array (n2). If yes then it means that, all the elements in the second array are present in the first array hence, second array is a subset of the first array.
Else, it means that, the second array is not the subset of first array. So, we display the same on the output console screen using predefined output function, printf().
if (count==n2)
printf(“\nArray 2 is a subset of Array 1\n “);
else
printf(“\nArray 2 is not a subset of Array 1\n”);
Check if An Array Is a Subset of Another Array – C Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include<stdio.h> int main() { int n1,n2,i,j,count=0; printf("\nEnter the number of elements in array 1 : "); scanf("%d", &n1); printf("\nEnter the number of elements in array 2 : "); scanf("%d", &n2); int arr1[n1],arr2[n2]; printf("\nEnter the %d positive elements in array 1 : ",n1); for(i=0;i<n1;i++) { scanf("%d",&arr1[i]); } printf("\nEnter the %d positive elements in array 2 : ",n2); for(i=0;i<n2;i++) { scanf("%d",&arr2[i]); } for (i = 0; i < n2; i++) { for (j = 0; j < n1; j++) { if(arr2[i] == arr1[j]) { count++; arr1[j]=-1; break; } } } if (count==n2) printf("\nArray 2 is a subset of Array 1\n "); else printf("\nArray 2 is not a subset of Array 1\n"); return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Enter the number of elements in array 1 : 5 Enter the number of elements in array 2 : 3 Enter the 5 positive elements in array 1 : 1 2 3 4 5 Enter the 3 positive elements in array 2 : 3 1 2 Array 2 is a subset of Array 1 |
Using Function
Above we have seen the logic behind determining whether an array is a subset of another array or not. But the disadvantage with having everything within main method is that, the code is not quite readable and also it is not reusable.
If some part of the code is to be found, we’ll have to go through the entire main method looking for it. This could be quite time consuming in case of lengthy codes. Also, if same logic is required elsewhere in the code then, we have to rewrite it.
To overcome this, we can make use of functions. We can split the code based on its functionality and place in within separate blocks. This way it looks a lot more systematic and enhances the readability of the code.
Apart from that, if same logic is required elsewhere then, we can just make a function call and pass the necessary parameter thereby, making it reusable.
To implement the same in this problem statement, we can first read all the necessary inputs using scanf() in the main method and then, make a function call to the user-defined function (checkSubset) by passing these inputs as parameters.
This function has the same logic discussed earlier and returns 1 if the count variable (c) is equal to the size (n2). Else, it returns 0. If the value returned to the main method is 1 then, we can display that the second array is subset of first else, we can display second array is not subset of first.
This way, the main method handles all the i.o functions while, the user-defined function (checkSubset) handles the logic.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
#include<stdio.h> int checkSubset(int a1[], int a2[], int n1, int n2) { int i = 0,j = 0,c=0; for (i = 0; i < n2; i++) { for (j = 0; j < n1; j++) { if(a2[i] == a1[j]) { c++; a1[j]=-1; break; } } } if (c==n2) return 1; return 0; } int main() { int n1,n2,i; printf("\nEnter the number of elements in array 1 : "); scanf("%d", &n1); printf("\nEnter the number of elements in array 2 : "); scanf("%d", &n2); int arr1[n1],arr2[n2]; printf("\nEnter the %d positive elements in array 1 : \n",n1); for(i=0;i<n1;i++) { scanf("%d",&arr1[i]); } printf("\nEnter the %d positive elements in array 2 : \n",n2); for(i=0;i<n2;i++) { scanf("%d",&arr2[i]); } if(checkSubset(arr1, arr2, n1, n2)) printf("\nArray 2 is a subset of Array 1\n "); else printf("\nArray 2 is not a subset of Array 1\n"); return 0; } |