How to check if two given sets are disjoint ? | A Simple C ++ program to check if two sets are disjoint ; Returns true if set1 [ ] and set2 [ ] are disjoint , else false ; Sort the given two sets ; Check for same elements using merge like process ; if set1 [ i ] == set2 [ j ] ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool areDisjoint ( int set1 [ ] , int set2 [ ] , int m , int n ) { sort ( set1 , set1 + m ) ; sort ( set2 , set2 + n ) ; int i = 0 , j = 0 ; while ( i < m && j < n ) { if ( set1 [ i ] < set2 [ j ] ) i ++ ; else if ( set2 [ j ] < set1 [ i ] ) j ++ ; else return false ; } return true ; } int main ( ) { int set1 [ ] = { 12 , 34 , 11 , 9 , 3 } ; int set2 [ ] = { 7 , 2 , 1 , 5 } ; int m = sizeof ( set1 ) / sizeof ( set1 [ 0 ] ) ; int n = sizeof ( set2 ) / sizeof ( set2 [ 0 ] ) ; areDisjoint ( set1 , set2 , m , n ) ? cout << " Yes " : cout << " ▁ No " ; return 0 ; }