Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find whether an array is subset of another array - Added Method 3 in C++
In this problem, we are given two arrays of integers arr1[] and arr2[] of size m and n. Our task is to find whether an array is subset of another array - Added Method 3.
Both arrays arr1[] and arr2[] are unorders and have distinct elements.
Let's take an example to understand the problem,
Input : arr1[] = {5, 2, 1, 6, 8, 10}, arr2[] = {6, 2, 1}
Output : arr2 is a subset of arr1.
Solution Approach
To solve this problem, we have discussed multiple methods here. Let's see the working of each of them with a program.
Method 1
One method to solve the problem is by directly checking for subsets. This is done using nested loops, outer for each element of the array arr2[] and inner one, for each element of the array arr1[]. We will check if each element of arr2 is present in arr1, if it is return 1 ( arr2 is subarray of arr1) otherwise return 0 (arr2 is not subarray of arr1).
Example
Program to illustrate the working of our solution
#include <iostream>
using namespace std;
bool isSubsetArray(int arr1[], int arr2[], int m, int n){
int j = 0;
for (int i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (arr2[i] == arr1[j])
break;
}
if (j == m)
return false;
}
return true;
}
int main(){
int arr1[] = {5, 2, 1, 6, 8, 10};
int arr2[] = {6, 2, 1};
int m = sizeof(arr1) / sizeof(arr1[0]);
int n = sizeof(arr2) / sizeof(arr2[0]);
isSubsetArray(arr1, arr2, m, n)? cout<<"arr2[] is subset of arr1[] ": cout<<"arr2[] is not a subset of arr1[]";
return 0;
}
Output
arr2[] is subset of arr1[]
Method 2
Another method to solve the problem is by checking if all elements of the arr2 are present in arr1. To do this effectively, we will sort the array arr1[] and then for each element of arr2, perform binary search to search for elements of arr2[] in arr1[]. Now, if any element is not found, return 0 (arr2 is not a subarray of arr1) and if all elements of arr2 are present in arr1, return 1 (arr2 is a subarray of arr1).
Example
Program to illustrate the working of our solution
#include <bits/stdc++.h>
using namespace std;
int binarySearch(int arr[], int low, int high, int x){
if (high >= low){
int mid = (low + high) / 2;
if ((mid == 0 || x > arr[mid - 1]) && (arr[mid] == x))
return mid;
else if (x > arr[mid])
return binarySearch(arr, (mid + 1), high, x);
else
return binarySearch(arr, low, (mid - 1), x);
}
return -1;
}
bool isSubsetArray(int arr1[], int arr2[], int m, int n){
int i = 0;
sort(arr1, arr1 + m);
for (i = 0; i < n; i++) {
if (binarySearch(arr1, 0, m - 1, arr2[i]) == -1)
return 0;
}
return 1;
}
int main(){
int arr1[] = {5, 2, 1, 6, 8, 10};
int arr2[] = {6, 2, 1};
int m = sizeof(arr1) / sizeof(arr1[0]);
int n = sizeof(arr2) / sizeof(arr2[0]);
isSubsetArray(arr1, arr2, m, n)? cout<<"arr2[] is subset of arr1[] ": cout<<"arr2[] is not a subset of arr1[]";
return 0;
}
Output
arr2[] is subset of arr1[]
Method 3
One more method to find the solution is by first sorting both the arrays arr1[] and arr2[]. Then for all elements of array arr2[] check if they are present in arr1[]. For this, we have a straight method which is using indexes of elements in both arrays.
Example
Program to illustrate the working of our solution
#include <bits/stdc++.h>
using namespace std;
bool isSubsetArray(int arr1[], int arr2[], int m, int n){
int i = 0, j = 0;
if (m < n)
return 0;
sort(arr1, arr1 + m);
sort(arr2, arr2 + n);
while (i < n && j < m){
if (arr1[j] < arr2[i])
j++;
else if (arr1[j] == arr2[i]){
j++;
i++;
}
else if (arr1[j] > arr2[i])
return 0;
}
return (i < n) ? false : true;
}
int main()
{
int arr1[] = {5, 2, 1, 6, 8, 10};
int arr2[] = {6, 2, 1};
int m = sizeof(arr1) / sizeof(arr1[0]);
int n = sizeof(arr2) / sizeof(arr2[0]);
isSubsetArray(arr1, arr2, m, n)? cout<<"arr2[] is subset of arr1[] ": cout<<"arr2[] is not a subset of arr1[]";
return 0;
}
Output
arr2[] is subset of arr1[]
Method 4
One more method, to check if arr2 is a subset of arr1 is using hashing. We will create a hash table using all the elements of arr1 and then search for elements of arr2 in the hash table. If values are found, then return 1 (arr2 is a subset of arr1) else return 0 (arr2 is not a subset of arr1).
Example
Program to illustrate the working of our solution
#include <bits/stdc++.h>
using namespace std;
bool isSubsetArray(int arr1[], int arr2[], int m, int n){
set<int> arr1Hash;
for (int i = 0; i < m; i++)
arr1Hash.insert(arr1[i]);
for (int i = 0; i < n; i++) {
if (arr1Hash.find(arr2[i]) == arr1Hash.end())
return false;
}
return true;
}
int main(){
int arr1[] = {5, 2, 1, 6, 8, 10};
int arr2[] = {6, 2, 1};
int m = sizeof(arr1) / sizeof(arr1[0]);
int n = sizeof(arr2) / sizeof(arr2[0]);
isSubsetArray(arr1, arr2, m, n)? cout<<"arr2[] is subset of arr1[] ": cout<<"arr2[] is not a subset of arr1[]";
return 0;
}
Output
arr2[] is subset of arr1[]
Method 5
One more method to solve the problem is using the set data structure. We will create a new set with all values of arr1 and check its length. Then we will try to insert all values of arr2, if adding changes the length then arr2 is not a subset of arr1. If no change in length occurs after adding elements of arr2 then arr2 is a subset of arr1.
Example
Program to illustrate the working of our solution
#include <bits/stdc++.h>
using namespace std;
bool isSubsetArray(int arr1[], int arr2[], int m, int n){
unordered_set<int> arrSet;
for (int i = 0; i < m; i++) {
arrSet.insert(arr1[i]);
}
int setSize = arrSet.size();
for (int i = 0; i < n; i++) {
arrSet.insert(arr2[i]);
}
if (arrSet.size() == setSize) {
return true;
}
else {
return false;
}
}
int main(){
int arr1[] = {5, 2, 1, 6, 8, 10};
int arr2[] = {6, 2, 1};
int m = sizeof(arr1) / sizeof(arr1[0]);
int n = sizeof(arr2) / sizeof(arr2[0]);
isSubsetArray(arr1, arr2, m, n)? cout<<"arr2[] is subset of arr1[] ": cout<<"arr2[] is not a subset of arr1[]";
return 0;
}
Output
arr2[] is subset of arr1[]