Check if two arrays are the disjoint or not

Problem statement:- Program to Check if two arrays are the disjoint or not.

Data requirement:-

   Input Data:- arr,size, arr2, size2

  Output Data:-string output

  Additional Data:- i, j, count

Program in C

Here is the source code of the C Program to Check if two arrays are the disjoint or not.

Code:

#include<stdio.h>
main()
{
    printf("Enter the size of the 1st array:");
    int size, i;
    scanf("%d",&size);
    int arr[size];

    printf("Enter the size of the 2nd array:");
    int size2;
    scanf("%d",&size2);
    int arr2[size2];

    printf("Enter the Element of the 1st array:\n");
     for(i=0;i<size;i++)
       {
        scanf("%d",&arr[i]);
        }

    printf("Enter the Element of the 2nd array:\n");
     for(i=0;i<size2;i++)
       {
        scanf("%d",&arr2[i]);
        }
    int j,count=0;
    for(i=0;i<size;i++)
    {
        for(j=0;j<size2;j++)
        {
            if(arr[i]==arr2[j]){
              count++;
        }}
    }
    if(count>=1)
        printf("Arrays are not disjoint.");
    else
        printf("Array are disjoint.");
}

Input/Output:
Enter the size of the 1st array:3
Enter the size of the 2nd array:4
Enter the Element of the 1st array:
5 4 6
Enter the Element of the 2nd array:
6 4 7 8
Arrays are not disjoint.

Program in C++

Here is the source code of the C++ Program to Check if two arrays are the disjoint or not.

Code:

#include<iostream>
using namespace std;
main()
{
    cout<<"Enter the size of the 1st array:";
    int size,i;
    cin>>size;
    int arr[size];

    cout<<"Enter the size of the 2nd array:";
    int size2;
    cin>>size2;
    int arr2[size2];

    cout<<"Enter the Element of the 1st array:\n";
     for(i=0;i<size;i++)
       {
        cin>>arr[i];
        }

    cout<<"Enter the Element of the 2nd array:\n";
     for(i=0;i<size2;i++)
       {
        cin>>arr2[i];
        }
    int j,count=0;


    for(i=0;i<size;i++)
    {
        for(j=0;j<size2;j++)
        {
            if(arr[i]==arr2[j]){
              count++;
        }}
    }
    if(count>=1)
        cout<<"Arrays are not disjoint.";
    else
        cout<<"Array are disjoint.";
}

Input/Output:
Enter the size of the 1st array:4
Enter the size of the 2nd array:3
Enter the Element of the 1st array:
5 4 6 7
Enter the Element of the 2nd array:
3 2 1
Array are disjoint.

Program in Java

Here is the source code of the Java Program to Check if two arrays are the disjoint or not.

Code:

import java.util.Scanner;
public class p30 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
    int size,j=0;
    size=sc.nextInt();
    System.out.println("Enter the size of the 2nd array:");
    int size2=sc.nextInt();

    int arr[ ]=new int[size];
    int arr2[ ]=new int[size2];
System.out.println("Enter the Element of the array:");
     for(j=0;j<size;j++)
    {
        arr[j]=sc.nextInt();
    }
     
     System.out.println("Enter the Element of the array:");
     for(j=0;j<size2;j++)
    {
        arr2[j]=sc.nextInt();
    }
     int count=0;
     for(int i=0;i<size;i++)
     {
         for(j=0;j<size2;j++)
         {
             if(arr[i]==arr2[j]){
               count++;
         }}
     }
     if(count>=1)
    System.out.println("Arrays are not disjoint.");
     else
    System.out.println("Array are disjoint.");
    sc.close();
}
}

Input/Output:
Enter the size of the array:
3
Enter the size of the 2nd array:
2
Enter the Element of the array:
4
5
6
Enter the Element of the array:
5
6
Arrays are not disjoint.

Program in Python

Here is the source code of the Python Program to Check if two arrays are the disjoint or not.

Code:

arr=[]
arr2=[]
size = int(input("Enter the size of the 1st array: "))
size2 = int(input("Enter the size of the 2nd array: "))

print("Enter the Element of the 1st array:")
for i in range(0,size):
    num = int(input())
    arr.append(num)

print("Enter the Element of the 2nd array:")
for i in range(0,size2):
    num2 = int(input())
    arr2.append(num2)

count=0
for i in range(0, size):
    for j in range(0, size2):
        if arr[i] == arr2[j]:
            count+=1

if count>=1:
    print("Arrays are not disjoint.")
else:
    print("Arrays are disjoint.")

Input/Output:
Enter the size of the 1st array: 2
Enter the size of the 2nd array: 3
Enter the Element of the 1st array:
4
5
Enter the Element of the 2nd array:
6
7
9
Arrays are disjoint.

Post a Comment

0 Comments