Programs


Title : Program to check whether an array is subset of another array.

#include <stdio.h>

int chkSubsetArray(int *arr1, int arr1_size, int *arr2, int arr2_size)

{

   int i, j;

   for (i = 0; i < arr2_size; i++)

   {

       for (j = 0; j < arr1_size; j++)

       {

           if (arr2[i] == arr1[j])

               break;

       }

       if (j == arr1_size)

           return 0;

   }

   return 1;

}

int main()

{

   int arr1[] = {5, 7, 4, 10, 5, 6, 1, 0, 2};

   int arr2[] = {4, 5, 0, 2, 1};

   int n1 = sizeof(arr1) / sizeof(arr1[0]);

   int i;

   int n2 = sizeof(arr2) / sizeof(arr2[0]);

     printf("The given first array is :  ");

   for (i = 0; i < n1; i++)

   {

       printf("%d  ", arr1[i]);

   }

   printf("\n");

     printf("The given second array is :  ");

   for (i = 0; i < n2; i++)

   {

       printf("%d  ", arr2[i]);

   }

   printf("\n");

     if (chkSubsetArray(arr1, 9, arr2, 4))

       printf("The second array is the subset of first array.");

   else

       printf("The second array is not a subset of first array");

   return 0;

}

OUTPUT:

The given first array is :  5  7  4  10  5  6  1  0  2 

The given second array is :  4  5  0  2  1 

The second array is the subset of first array.

Related Topics

  • Integer x appears more than n by 2 times in sorted array

      Jan 15, 2022

  • Program to find majority element of an array

      Jan 03, 2022

  • Program to print a matrix in spiral form

      Jan 03, 2022

  • WAP to find the maximum circular sub array sum of given array

      Jan 03, 2022

  • Program to count the number of triangles from a given array

      Jan 03, 2022

  • WAP to find the number of times occurs a given number in an array

      Jan 03, 2022

  • Subscribe Now