given two arrays: Arr1[0..m-1] and arr2[0..n-1]. Determines whether arr2[] is a subset of arr1[]. Both of these arrays are unordered.
For example:
Input: arr1[] = {One, 1, 3, 7}, arr2[] = {11, 3, 7, 1}
Output: ARR2 is a subset of the arr1.
Input: arr1[] = {1, 2, 3, 4, 5, 6}, arr2[] = {1, 2, 4}
Output: ARR2 is a subset of the arr1.
Input: arr1[] = {Ten, 5, 2, Max, +}, arr2[] = {19, 5, 3}
Output: ARR2 is not a subset of ARR1 because element 3 in ARR2 does not exist in arr1.
Method 1 (Simple method):
#include < iostream>//if ARR2 is a subset of arr1, then return 1.bool issubset (int arr1[], int arr2[], int numArr1, int numArr2) {int i = 0; int j = 0; for (i = 0; i < numArr2; i++) {for (j = 0; J < NumArr1; J + +) {if (arr2[i] = = Arr1[j]) break; }//If the inner loop above has no break, then ARR2 is not a subset of arr1 if (j = = numArr1) return 0; }//If performed here, the description arr2 is a subset of arr1 return 1;} int main () {int arr1[] = {11, 1, 13, 21, 3, 7}; int arr2[] = {11, 3, 7, 1}; int numArr1 = sizeof (ARR1)/sizeof (arr1[0]); int numArr2 = sizeof (ARR2)/sizeof (arr2[0]); if (Issubset (arr1, arr2, NUMARR1, NUMARR2)) std::cout<< "arr2[] is subset of arr1[]"; else std::cout<< "arr2[" is not a subset of arr1[] "; return 0;}
Complexity of Time: O (m*n)
Method 2 (Use sort and binary search)
1) Sort arr1[], average O (MLOGM)
2) for each element in arr2[], find the binary in the sorted arr1[].
A) If this element is not found, 0 is returned.
3) returns 1 if all elements are found.
#include the <iostream>//function declaration. Two auxiliary functions for determining a subset. void QuickSort (int *arr, int si, int ei), int binarysearch (int arr[], int low, int high, int x);//If arr2[] is a subset of arr1[], return 1bool issubset (int arr1[], int arr2[], int numArr1, int numArr2) {int i = 0; QuickSort (arr1, 0, numarr1-1); for (i=0; i<numarr2; i++) {if (BinarySearch (arr1, 0, Numarr1-1, arr2[i]) = =-1) return 0; }//If performed here, the description arr2 is a subset of arr1 return 1;} ---------Auxiliary function begin--------//Standard binary lookup function int binarysearch (int arr[], int low, int high, int x) {if (high >= low) {in T mid = (low + high)/2; or Low + (high-low)/2; /* * Detect Arr[mid] is the first time an X is encountered. * When X meets one of the following conditions, it proves to be the first encounter with X: (1) (Mid = = 0) && (arr[mid] = = x) (2) (Arr[mid-1] < x) && (Arr[mid] = = x) */if ((Mid = = 0 | | arr[mid-1] < x) && (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;} Template<typename type>void Exchange (Type *a, type *b) {type temp; temp = *a; *a = *b; *b = temp;} int partition (int a[], int si, int ei) {int x = A[ei]; int i = (si-1); Int J; for (j = si; J <= Ei-1; j + +) {if (A[j] <= x) {i++; Exchange (&a[i], &a[j]); }} Exchange (&a[i + 1], &a[ei]); return (i + 1);} /* Implement quick Sort function a[]: Array to be sorted si:starting indexei:ending index*/void quickSort (int a[], int si, int ei) {int pi; Partitioning index if (Si < ei) {pi = partition (A, si, ei); QuickSort (A, si, pi-1); QuickSort (A, pi + 1, ei); }}//---------auxiliary function End--------int main () {int arr1[] = {11, 1, 13, 21, 3, 7}; int arr2[] = {11, 3, 7, 1}; int numArr1 = sizeof (ARR1)/sizeof (arr1[0]); int numArr2 = sizeof (ARR2)/sizeof (arr2[0]); if (Issubset (arr1, arr2, NUMARR1, NUMARR2)) std::cout<< "arr2[] is subset of arr1[]"; else std::cout<< "arr2[" is not a subset of arr1[] "; return 0;}
Time complexity: O (MLOGM + NLOGM). Among them, MLOGM is the average complexity of the sorting algorithm. Because the above uses a quick sort, and if it is the worst case, the complexity becomes O (m^2).
Method 3 (Use sort and merge)
1) sort the two arrays arr1 and ARR2 respectively. O (MLOGM + nlogn)
2) Use the merge process to detect whether an ordered array arr2 exists in the ordered arr1.
If ARR2 is a subset of arr1, it returns 1bool issubset (int arr1[], int arr2[], int m, int n) { int i = 0, j = 0; if (M < n) return 0; QuickSort (arr1, 0, m-1); QuickSort (arr2, 0, n-1); 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; } if (i < n) return 0; else return 1;}
Time complexity: O (MLOGM + nlogn). Better than Method 2.
Method 4 (Use hash)
1) Create a hash table for all elements of the array arr1.
2) iterate through the array arr2 and detect if each of these elements exists in the hash table. Returns 0 if no element is found in the hash table.
3) returns 1 if all elements have been found.
Note: The method 1,2,4 does not handle cases where there are duplicate elements in the ARR2 array. For example, {1, 4, 4, 2} are not actually subsets of {1, 4, 2}, but these methods are considered subsets.
Hash (3)-Determines whether an array is a subset of another array