Check if one array is a subset of another array or not

Problem Statement:- Program to Check if one array is a subset of another array or not.

Data requirement:-

   Input Data:- arr,size,are2,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 one array is a subset of another array 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==size2)
        printf("Array two is a subset of array one.");
    else
        printf("Array two is not a subset of array one");
}

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:
2 5 3 6
Enter the Element of the 2nd array:
6 5 2
Array two is a subset of array one.

Program in C++

Here is the source code of the C++ Program to Program to Check if one array is a subset of another array 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==size2)
        cout<<"Array two is a subset of array one.";
    else
        cout<<"Array two is not a subset of array one";
}

Input/Output:
Enter the size of the 1st array:5
Enter the size of the 2nd array:6
Enter the Element of the 1st array:
4 55 66 92 78
Enter the Element of the 2nd array:
7 9 20 145 7 9 8
Array two is not a subset of array one

Program in Java

Here is the source code of the Java Program to Program to Check if one array is a subset of another array or not.

Code:

import java.util.Scanner;
public class p31 {

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 i,count=0;


     for(i=0;i<size;i++)
     {
         for(j=0;j<size2;j++)
         {
             if(arr[i]==arr2[j]){
               count++;
         }}
     }
     if(count==size2)
    System.out.println("Array two is a subset of array one.");
     else
    System.out.println("Array two is not a subset of array one");     
     sc.close();
}
}

Input/Output:
Enter the size of the array:
3
Enter the size of the 2nd array:
4
Enter the Element of the array:
5
9
7
Enter the Element of the array:
5
9
7
9
Array two is a subset of array one.

Program in Python

Here is the source code of the Python Program to Program to Check if one array is a subset of another array 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==size2:
    print("Array two is a subset of array one.")
else:
    print("Array two is not a subset of array one.")

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
9
17
Enter the Element of the 2nd array:
9
17
4
Array two is a subset of array one.

Post a Comment

0 Comments