Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
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
#includeusing namespace std; bool isSubsetArray(int arr1[], int arr2[], int m, int n){ int j = 0; for (int i = 0; i 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
#includeusing 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 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
#includeusing namespace std; bool isSubsetArray(int arr1[], int arr2[], int m, int n){ int i = 0, j = 0; if (m arr2[i]) return 0; } return (i 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
#includeusing namespace std; bool isSubsetArray(int arr1[], int arr2[], int m, int n){ set arr1Hash; for (int i = 0; i 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
#includeusing namespace std; bool isSubsetArray(int arr1[], int arr2[], int m, int n){ unordered_set arrSet; for (int i = 0; i Output
arr2[] is subset of arr1[]
