Check for Disjoint Arrays or Sets
Given two arrays a and b, check if they are disjoint, i.e., there is no element common between both the arrays.
Examples:
Input: a[] = {12, 34, 11, 9, 3}, b[] = {2, 1, 3, 5}
Output: False
Explanation: 3 is common in both the arrays.Input: a[] = {12, 34, 11, 9, 3}, b[] = {7, 2, 1, 5}
Output: True
Explanation: There is no common element in both the sets.
Table of Content
[Naive Approach] Using Two Nested Loops - O(n x m) Time and O(1) Space
The simplest approach is to iterate through every element of the first array and search it in another array, if any element is found, the given arrays are not disjoint.
// C++ program to check if the given arrays are disjoint
// using two nested loops
#include <iostream>
#include <vector>
using namespace std;
bool areDisjoint(vector<int> &a, vector<int> &b) {
// Take every element of array a
// and search it in array b
for(int i = 0; i < a.size(); i++) {
for(int j = 0; j < b.size(); j++) {
// If any common element is found
// given arrays are not disjoint
if(a[i] == b[j])
return false;
}
}
return true;
}
int main() {
vector<int> a = {12, 34, 11, 9, 3};
vector<int> b = {7, 2, 1, 5};
if(areDisjoint(a, b))
cout << "True";
else
cout << "False";
return 0;
}
// C program to check if the given arrays are disjoint
// using two nested loops
#include <stdio.h>
int areDisjoint(int a[], int n, int b[], int m) {
// Take every element of array a
// and search it in array b
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
// If any common element is found
// given arrays are not disjoint
if(a[i] == b[j])
return 0;
}
}
return 1;
}
int main() {
int a[] = {12, 34, 11, 9, 3};
int b[] = {7, 2, 1, 5};
int n = sizeof(a) / sizeof(a[0]);
int m = sizeof(b) / sizeof(b[0]);
if(areDisjoint(a, n, b, m))
printf("True");
else
printf("False");
return 0;
}
// Java program to check if the given arrays are disjoint
// using two nested loops
import java.util.*;
class GfG {
static boolean areDisjoint(int[] a, int[] b) {
// Take every element of array a
// and search it in array b
for(int i = 0; i < a.length; i++) {
for(int j = 0; j < b.length; j++) {
// If any common element is found
// given arrays are not disjoint
if(a[i] == b[j])
return false;
}
}
return true;
}
public static void main(String[] args) {
int[] a = {12, 34, 11, 9, 3};
int[] b = {7, 2, 1, 5};
if(areDisjoint(a, b))
System.out.println("True");
else
System.out.println("False");
}
}
# Python program to check if the given arrays are disjoint
# using two nested loops
def areDisjoint(a, b):
# Take every element of array a
# and search it in array b
for i in range(len(a)):
for j in range(len(b)):
# If any common element is found
# given arrays are not disjoint
if a[i] == b[j]:
return False
return True
a = [12, 34, 11, 9, 3]
b = [7, 2, 1, 5]
if areDisjoint(a, b):
print("True")
else:
print("False")
// C# program to check if the given arrays are disjoint
// using two nested loops
using System;
class GfG {
static bool AreDisjoint(int[] a, int[] b) {
// Take every element of array a
// and search it in array b
for(int i = 0; i < a.Length; i++) {
for(int j = 0; j < b.Length; j++) {
// If any common element is found
// given arrays are not disjoint
if(a[i] == b[j])
return false;
}
}
return true;
}
static void Main() {
int[] a = {12, 34, 11, 9, 3};
int[] b = {7, 2, 1, 5};
if(AreDisjoint(a, b))
Console.WriteLine("True");
else
Console.WriteLine("False");
}
}
// JavaScript program to check if the given arrays are disjoint
// using two nested loops
function areDisjoint(a, b) {
// Take every element of array a
// and search it in array b
for (let i = 0; i < a.length; i++) {
for (let j = 0; j < b.length; j++) {
// If any common element is found
// given arrays are not disjoint
if (a[i] === b[j])
return false;
}
}
return true;
}
const a = [12, 34, 11, 9, 3];
const b = [7, 2, 1, 5];
if (areDisjoint(a, b))
console.log("True");
else
console.log("False");
Output
True
[Better Approach] Using Merge of Merge Sort - O(n Log n + m Log m) Time and O(1) Space
First, we sort both the arrays. Then, we initialize pointers at the beginning of both the arrays. If the value at one pointer is smaller than the value at the other, we increment that pointer. If at any point, the values at both pointers become equal, the arrays are not disjoint. If no value matches during the whole traversal, then the given arrays are disjoint.
// C++ program to check if the given arrays are disjoint
// using merge of merge sort
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool areDisjoint(vector<int> &a, vector<int> &b) {
// Sorting both the arrays
sort(a.begin(), a.end());
sort(b.begin(), b.end());
// Initializing pointers at the
// beginning of both the arrays
int i = 0, j = 0;
while(i < a.size() && j < b.size()) {
// If any common element is found, then
// given arrays are not disjoint
if(a[i] == b[j])
return false;
// Incrementing the pointer
// having smaller value
if(a[i] < b[j])
i++;
else
j++;
}
return true;
}
int main() {
vector<int> a = {12, 34, 11, 9, 3};
vector<int> b = {7, 2, 1, 5};
if(areDisjoint(a, b))
cout << "True";
else
cout << "False";
return 0;
}
// C program to check if the given arrays are disjoint
// using merge of merge sort
#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
int areDisjoint(int a[], int n, int b[], int m) {
// Sorting both the arrays
qsort(a, n, sizeof(int), compare);
qsort(b, m, sizeof(int), compare);
// Initializing pointers at the
// beginning of both the arrays
int i = 0, j = 0;
while(i < n && j < m) {
// If any common element is found, then
// given arrays are not disjoint
if(a[i] == b[j])
return 0;
// Incrementing the pointer
// having smaller value
if(a[i] < b[j])
i++;
else
j++;
}
return 1;
}
int main() {
int a[] = {12, 34, 11, 9, 3};
int b[] = {7, 2, 1, 5};
int n = sizeof(a) / sizeof(a[0]);
int m = sizeof(b) / sizeof(b[0]);
if(areDisjoint(a, n, b, m))
printf("True");
else
printf("False");
return 0;
}
// Java program to check if the given arrays are disjoint
// using merge of merge sort
import java.util.Arrays;
class GfG {
static boolean areDisjoint(int[] a, int[] b) {
// Sorting both the arrays
Arrays.sort(a);
Arrays.sort(b);
// Initializing pointers at the
// beginning of both the arrays
int i = 0, j = 0;
while(i < a.length && j < b.length) {
// If any common element is found, then
// given arrays are not disjoint
if(a[i] == b[j])
return false;
// Incrementing the pointer
// having smaller value
if(a[i] < b[j])
i++;
else
j++;
}
return true;
}
public static void main(String[] args) {
int[] a = {12, 34, 11, 9, 3};
int[] b = {7, 2, 1, 5};
if(areDisjoint(a, b))
System.out.println("True");
else
System.out.println("False");
}
}
# Python program to check if the given arrays
# are disjoint using merge of merge sort
def areDisjoint(a, b):
# Sorting both the arrays
a.sort()
b.sort()
# Initializing pointers at the
# beginning of both the arrays
i, j = 0, 0
while i < len(a) and j < len(b):
# If any common element is found, then
# given arrays are not disjoint
if a[i] == b[j]:
return False
# Incrementing the pointer
# having smaller value
if a[i] < b[j]:
i += 1
else:
j += 1
return True
a = [12, 34, 11, 9, 3]
b = [7, 2, 1, 5]
if areDisjoint(a, b):
print("True")
else:
print("False")
// C# program to check if the given arrays are disjoint
// using merge of merge sort
using System;
class GfG {
static bool AreDisjoint(int[] a, int[] b) {
// Sorting both the arrays
Array.Sort(a);
Array.Sort(b);
// Initializing pointers at the
// beginning of both the arrays
int i = 0, j = 0;
while(i < a.Length && j < b.Length) {
// If any common element is found, then
// given arrays are not disjoint
if(a[i] == b[j])
return false;
// Incrementing the pointer
// having smaller value
if(a[i] < b[j])
i++;
else
j++;
}
return true;
}
static void Main() {
int[] a = {12, 34, 11, 9, 3};
int[] b = {7, 2, 1, 5};
if(AreDisjoint(a, b))
Console.WriteLine("True");
else
Console.WriteLine("False");
}
}
// JavaScript program to check if the given arrays are disjoint
// using merge of merge sort
function areDisjoint(a, b) {
// Sorting both the arrays
a.sort((x, y) => x - y);
b.sort((x, y) => x - y);
// Initializing pointers at the
// beginning of both the arrays
let i = 0, j = 0;
while(i < a.length && j < b.length) {
// If any common element is found, then
// given arrays are not disjoint
if(a[i] === b[j])
return false;
// Incrementing the pointer
// having smaller value
if(a[i] < b[j])
i++;
else
j++;
}
return true;
}
const a = [12, 34, 11, 9, 3];
const b = [7, 2, 1, 5];
if (areDisjoint(a, b))
console.log("True");
else
console.log("False");
Output
True
[Expected Approach] Using Hashing - O(n + m) Time and O(n) Space
First, we insert all elements of array a into a hash set. Then, for each element of array b, we check if it exists in the hash set. If any element from array b is found in the hash set, the arrays are not disjoint. otherwise, they are disjoint.
// C++ program to check if the given arrays are disjoint
// using hashing
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
bool areDisjoint(vector<int> &a, vector<int> &b) {
unordered_set<int> st;
// Insert all elements of array
// a into the hash set
for (int ele : a)
st.insert(ele);
for (int ele : b) {
// If an element from b is found in the
// hash set, arrays are not disjoint
if (st.find(ele) != st.end())
return false;
}
return true;
}
int main() {
vector<int> a = {12, 34, 11, 9, 3};
vector<int> b = {7, 2, 1, 5};
if (areDisjoint(a, b))
cout << "True";
else
cout << "False";
return 0;
}
// Java program to check if the given arrays are disjoint
// using hashing
import java.util.HashSet;
class GfG {
static boolean areDisjoint(int[] a, int[] b) {
HashSet<Integer> st = new HashSet<>();
// Insert all elements of array
// a into the hash set
for (int ele : a)
st.add(ele);
for (int ele : b) {
// If an element from b is found in the
// hash set, arrays are not disjoint
if (st.contains(ele))
return false;
}
return true;
}
public static void main(String[] args) {
int[] a = {12, 34, 11, 9, 3};
int[] b = {7, 2, 1, 5};
if (areDisjoint(a, b))
System.out.println("True");
else
System.out.println("False");
}
}
# Python program to check if the given arrays are disjoint
# using hashing
def areDisjoint(a, b):
st = set()
# Insert all elements of array
# a into the hash set
for ele in a:
st.add(ele)
for ele in b:
# If an element from b is found in the
# hash set, arrays are not disjoint
if ele in st:
return False
return True
a = [12, 34, 11, 9, 3]
b = [7, 2, 1, 5]
if areDisjoint(a, b):
print("True")
else:
print("False")
// C# program to check if the given arrays are disjoint
// using hashing
using System;
using System.Collections.Generic;
class GfG {
static bool AreDisjoint(int[] a, int[] b) {
HashSet<int> st = new HashSet<int>();
// Insert all elements of array
// a into the hash set
foreach (int ele in a)
st.Add(ele);
foreach (int ele in b) {
// If an element from b is found in the
// hash set, arrays are not disjoint
if (st.Contains(ele))
return false;
}
return true;
}
static void Main() {
int[] a = {12, 34, 11, 9, 3};
int[] b = {7, 2, 1, 5};
if (AreDisjoint(a, b))
Console.WriteLine("True");
else
Console.WriteLine("False");
}
}
// JavaScript program to check if the given arrays are disjoint
// using hashing
function areDisjoint(a, b) {
const st = new Set();
// Insert all elements of array
// a into the hash set
for (let ele of a)
st.add(ele);
for (let ele of b) {
// If an element from b is found in the
// hash set, arrays are not disjoint
if (st.has(ele))
return false;
}
return true;
}
const a = [12, 34, 11, 9, 3];
const b = [7, 2, 1, 5];
if (areDisjoint(a, b))
console.log("True");
else
console.log("False");
Output
True