C++ program to find Arrays are disjoint or not

Array is disjoint or not

Here is the C++ program and it’s explanation to find Arrays are disjoint or not . Two arrays are known to be disjoint when they have non repeating elements in both the arrays

Example

arr1 = {1,2,3,4,5}   arr2 = {6,7,8,9,10}

arr1 & arr2 are disjoint

arr3 = {1,2,3,4,5}   arr4 = {3,2,8,9,10}
arr3 & arr4 are not disjoint as they have 2,3 are repeating elements

C++ program to find Arrays are disjoint or not

Algorithm

  1. Use two loops.
  2. Traverse the array 1 using the outer loop.
  3. Use the inner loop to check if the elements in array 2 are found in array 1.
  4. If at least one element of array 2 is found in array 1, return false. Else return true.

C++ program to find Arrays are disjoint or not :-

 

    
    #include <iostream>
    using namespace std;
    int disjoint_arrays(int array1[], int array2[], int yint m){
            int i,j;
            for(i = 0;i<y;i++){
                for(j=0;j<z;j++){
                    if(array1[i] == array2[j])
                        return –1;
                }
            }
            return 1;
    }
        
    void main (String[] args){
        int y,z,i;
        int arr1[] = new int[10];
        int arr2[] = new int[10];
        cout<<“Enter the size of array 1 : “;
        cin>>m;
        cout<<\nEnter the size of array 2 : “ ;
        cin>>n;
        cout<<\nInput the array 1 elements : “;
        for(i=0;i<y;i++){
                cin>>array1[i];
        }
        cout<<\nInput the array 2 elements : “;
        for(i=0;i<z;i++){
            cin>>array2[i];
        }
        int res = disjoint_arrays(arr1,arr2,n,m);
        if(res == –1)
            cout<<“The arrays are not disjoint”;
        else
            cout<<“The arrays are disjoint”;
        }
    }
    
    Output
    Enter size of array 1 : 5
    Enter size of array 1 : 5
    input array 11,2,3,4,5
    input array 26,7,8,9,0
    array is disjoint