Write Program to find whether Arrays are disjoint or not

28 September 2022

Write Program to find whether Arrays are disjoint or not


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Discription

Sample input 1:

4

2 -4 -1 -3

3

1 3 5

Sample output 1:

Disjoint

Sample input 2:

5

1 5 -7 6 3

4

2 4 6 8

Sample output 2:

Not disjoint. ( 6 is common)

 

Solution:

C Program

#include<stdio.h>

 

int DisjointOrNot(int *arr1, int *arr2, int n1, int n2)

{

    int flag = 1;

    for (int i=0; i<n1; i++)

    {

        for (int j=0; j<n2; j++)

        {

            if (arr1[i] == arr2[j])

            {

                flag = 0;;

            }

        }

        if(flag == 0)

        {

            break;

        }

    }

    return flag;

}

 

int main()

{

       int n1,n2;

       scanf("%d",&n1);

       int arr1[n1];

       for(int i = 0 ; i<n1 ; i++)

       {

               scanf("%d",&arr1[i]);

       }

       scanf("%d",&n2);

       int arr2[n2];

       for(int i = 0 ; i<n2 ; i++)

       {

               scanf("%d",&arr2[i]);

       }

       if(DisjointOrNot(arr1,arr2,n1,n2) == 1)

       {

           printf("Disjoint");

       }

       else

       {

           printf("Not disjoint");

       }

       return 0;

}

 

C++ Program

#include<bits/stdc++.h>

using namespace std;

 

bool DisjointOrNot(int arr1[], int arr2[], int n1, int n2)

{

    bool flag = true;

    for (int i=0; i<n1; i++)

    {

        for (int j=0; j<n2; j++)

        {

            if (arr1[i] == arr2[j])

            {

                flag = false;;

            }

        }

        if(flag == false)

        {

            break;

        }

    }

    return flag;

}

 

int main()

{

       int n1,n2;

       cin>>n1;

       int arr1[n1];

       for(int i = 0 ; i<n1 ; i++)

       {

               cin>>arr1[i];

       }

       cin>>n2;

       int arr2[n2];

       for(int i = 0 ; i<n2 ; i++)

       {

               cin>>arr2[i];

       }

       if(DisjointOrNot(arr1,arr2,n1,n2))

       {

           cout<<"Disjoint";

       }

       else

       {

           cout<<"Not disjoint";

       }

       return 0;

}

 

JAVA Program

import java.util.*;

class Main

{

   static boolean DisjointOrNot(int arr1[], int arr2[], int n1, int n2)

    {

        boolean flag = true;

        for (int i=0; i<n1; i++)

        {

            for (int j=0; j<n2; j++)

            {

                if (arr1[i] == arr2[j])

                {

                    flag = false;;

                }

            }

            if(flag == false)

            {

                break;

            }

        }

        return flag;

    }

  

       public static void main(String[] args) throws java.lang.Exception

       {

               Scanner sc = new Scanner(System.in);

               int n1 = sc.nextInt();

               int arr1[] = new int[n1];

               for(int i = 0 ; i<n1 ; i++)

               {

                             arr1[i] = sc.nextInt();

               }

               int n2 = sc.nextInt();

               int arr2[] = new int[n2];

               for(int i = 0 ; i<n2 ; i++)

               {

                             arr2[i] = sc.nextInt();

               }

               if(DisjointOrNot(arr1,arr2,n1,n2)==true)

           {

               System.out.print("Disjoint");

           }

           else

           {

               System.out.print("Not disjoint");

           }

       }

}

 

PYTHON Program

def DisjointOrNot(arr1,arr2,n1,n2):

    flag = True;

    for i in range(0,n1):

        for j in range(0,n2):

            if arr1[i] == arr2[j] :

                flag = False

        if flag == False :

            break

    return flag

 

n1 = int(input())

arr1 = list(map(int,input().split(' ')))

n2 = int(input())

arr2 = list(map(int,input().split(' ')))

if DisjointOrNot(arr1,arr2,n1,n2) == True :

    print("Disjoint")

else:

    print("Not disjoint")

If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023
Related Articles

Ask Us Anything !