Java program to find Arrays are disjoint or not

Arrays are Disjoint or Not in Java

Here, in this page we will discuss the program to find whether the given arrays are disjoint or not in java programming language. Two arrays are said to be disjoint if the intersection of the array is empty.

Disjoint array java

Method Discussed :

  • Method 1 : Using Naive approach.
  • Method 2 : Using sorting and hashing
  • Method 3 : Using Hashing

Method 1 :

  • Run a loop from index 0 to n
  • Run a nested loop from 0 to m
  • Check if (arr1[i]==arr2[j]) return 0.
  • Otherwise, return 1.

Time and Space Complexity :

  • Time Complexity : O(n2)
  • Space Complexity : O(1)

Method 1 : Code in Java

import java.util.*;
 
class Main
{
    // This function prints all distinct elements
    static boolean Disjoint(int arr1[], int arr2[])
    {
        
        for(int i=0; i<arr1.length; i++){
        for(int j=0; j<arr2.length; j++){
            if(arr1[i]==arr2[j]){
                return false;   
            }
        }
    }
    return true;
    }
 
    // Driver method to test above method
    public static void main (String[] args)
    {
        int arr1[] = {10, 51, 3, 43, 6};
        int arr2[] = {80, 71, 29, 3};
        
        if (Disjoint(arr1, arr2))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}

Output :

No

Method 2 :

In this method first sort both the given integer arrays, then using the merge process we compare the elements in both arrays.

Method 2 : Code in Java

import java.util.*;
 
class Main
{
    // This function prints all distinct elements
    static boolean Disjoint(int arr1[], int arr2[])
    {
       int i=0,j=0;
         
        // Sort the given two sets
        Arrays.sort(arr1);
        Arrays.sort(arr2);
         
        // Check for same elements using
        // merge like process
        while(iarr2[j])
                j++;
            else
                return false;
             
        }
        return true;
    }
 
    // Driver method to test above method
    public static void main (String[] args)
    {
        int arr1[] = {10, 51, 3, 43, 6};
        int arr2[] = {80, 71, 29, 3};
        
        if (Disjoint(arr1, arr2))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}    

Output :

No

Method 3 :

In this method we use the concept of hashing.

  • Iterate through the first array and store every element in the hash table.
  • Iterate through the second array and check if any element is present in the hash table.
  • If present, then returns false, else ignore the element. 
  • If all elements of the second array are not present in the hash table, return true.

Method 3 : Code in Java

import java.util.*;
 
class Main
{
    // This function prints all distinct elements
    static boolean Disjoint(int arr1[], int arr2[])
    {
       HashSet set = new HashSet<>();
 
        // Traverse the first set and store its elements in hash
        for (int i=0; i<arr1.length; i++)
            set.add(arr1[i]);
 
        // Traverse the second set and check if any element of it
        // is already in hash or not.
        for (int i=0; i<arr2.length; i++)
            if (set.contains(arr2[i]))
                return false;
 
        return true;
    }
 
    // Driver method to test above method
    public static void main (String[] args)
    {
        int arr1[] = {10, 51, 3, 43, 6};
        int arr2[] = {80, 71, 29, 3};
        
        if (Disjoint(arr1, arr2))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}    

Output :

No

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

3 comments on “Java program to find Arrays are disjoint or not”


  • Ayushi

    #Python Program
    arr1 = list(map(int, input(“Enter array 1: “).split()))
    arr2 = list(map(int, input(“Enter array 2: “).split()))

    count = 0
    for i in range(len(arr1)):
    for j in range(len(arr2)):
    if arr1[i] == arr2[j]:
    print(“Not Disjoint”)
    count+=1
    break
    if count==0:
    print(“Disjoint array”)


    • Ayushi

      arr1 = list(map(int, input(“Enter array 1: “).split()))
      arr2 = list(map(int, input(“Enter array 2: “).split()))

      count = 0
      for i in arr1:
      for j in arr2:
      if i == j:
      print(“Not Disjoint”)
      count+=1
      break
      if count==0:
      print(“Disjoint array”)


  • Mahesh

    int[]a1={1,2,3,4,5};
    int []a2={4,5,8,9,0};
    int size=a1.length;
    String ans=”Disjoint array”;
    Arrays.sort(a2);
    for(int i=0;i<size;i++){
    if(search(a2,a1[i])){
    ans="Not Disjoint array";
    break;
    }
    }
    System.out.println(ans);
    }
    static boolean search(int[]a,int x){
    int low=0;
    int high=a.length-1;
    while(lowx){
    high=mid-1;
    }else{
    low=mid+1;
    }
    }
    return false;
    }