# Write a program to find whether an array is a subset of another array or not.

07 January 2023

Write a program to find whether an array is a subset of another array 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

C Program

#include <stdio.h>

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

{

int flag = 1,i,j;

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

{

for (j = 0; j < n1; j++)

{

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

break;

}

if (j == n1)

{

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[n2]);

}

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

{

cout<<"Subset";

}

else

{

cout<<"Not subset";

}

return 0;

}

C++ Program

#include<bits/stdc++.h>

using namespace std;

int SubsetOrNot(int arr1[], int arr2[],int n1, int n2)

{

int flag = 1,i,j;

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

{

for (j = 0; j < n1; j++)

{

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

break;

}

if (j == n1)

{

flag = 0;

break;

}

}

return flag;

}

int main()

{

int n1,n2;

cin>>n;

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>>arr1[i];

}

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

{

cout<<"Subset";

}

else

{

cout<<"Not subset";

}

return 0;

}

JAVA Program

import java.util.*;

class Main

{

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

{

boolean flag = true;

int i,j;

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

{

for (j = 0; j < n1; j++)

{

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

break;

}

if (j == n1)

{

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(SubsetOrNot(arr1,arr2,n1,n2)==true)

{

System.out.print("Subset");

}

else

{

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

}

}

}

PYTHON Program

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

i = 0

j = 0

for i in range(n2):

for j in range(n1):

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

break

if j == n1:

return 0

return 1

n1 = int(input())

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

n2 = int(input())

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

if (SubsetOrNot(arr1,arr2,n1,n2)):

print("Subset")

else:

print("Not Subset")

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