Check Arrays are disjoint or not

Write a program to  Check Arrays are disjoint or not.

Two arrays are said to be disjoint if they have no elements in common. 

For example : arr1 = {1,2,3,4,5} arr2 = {6,7,8,9} arr1 and arr2 elements are unique and hence they are disjoint

Example:

Input:  arr1[]={1,2,3,4,5}, arr2[]={6,7,8,9}
Output: Arrays are disjoint

C Program

#include <stdio.h>
#include <stdbool.h>

//function to check for disjoint arrays
bool disjointArray(int arr1[]int nint arr2[]int m)
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (arr1[i] == arr2[j])
            {
                return false;
            }
        }
    }
    return true;
}
int main()
{
    int arr1[] = {12345};
    int arr2[] = {6789};

    //find length of both the arrays
    int n = sizeof(arr1) / sizeof(arr1[0]);
    int m = sizeof(arr2) / sizeof(arr2[0]);

    if (disjointArray(arr1, n, arr2, m))
    {
        printf("Arrays are disjoint\n");
    }
    else
    {
        printf("Arrays are not disjoint\n");
    }

    return 0;
}


No comments:

Post a Comment