











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


Algorithm
- Use two loops.
- Traverse the array 1 using the outer loop.
- Use the inner loop to check if the elements in array 2 are found in array 1.
- 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 y, int 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 1: 1,2,3,4,5
input array 2: 6,7,8,9,0
array is disjoint
Login/Signup to comment