## Find whether an array is subset of another arrayTwo arrays, arr1[0..m-1] and arr2[0..n-1], are provided. Determine whether or whether arr2[] is a subset of arr1[]. The two arrays are not ordered in any way. It is possible to assume that each element in both arrays is unique. ## EXAMPLES
arr2[] is a subset of arr1[]
arr2[] is a subset of arr1[]
arr2[] is not a subset of arr1[] ## Simple Way to Check whether One Array Is a Subset of Another ArrayUtilize two loops: The outer loop selects each member of arr2[] individually. The element chosen by the outer loop is looked for linearly by the inner loop. Return 1 if all elements were located; otherwise, return 0. The application of the aforementioned strategy is seen below: ## CODE
arr2[] is subset of arr1[]
## Utilize sorting and binary search to determine whether one array is a subset of another array.The plan is to sort the provided array arr1[] before conducting a binary search for each member in arr2[] in the sorted arr1[]. Return 0 if the element cannot be found. Return 1 if all elements are present. ## Illustration:Given array arr1[] = { 11, 1, 13, 21, 3, 7 } and arr2[] = { 11, 3, 7, 1 }. Step 1: We will sort the array arr1[], and have arr1[] = { 1, 3, 7, 11, 13, 21}. Step 2: We will look for each element in arr2[] in arr1[] using binary search. - arr2[] = { 11, 3, 7, 1 }, 11 is present in arr1[] = { 1, 3, 7, 11, 13, 21}
- arr2[] = { 11, 3, 7, 1 }, 11 is present in arr1[] = { 1, 3, 7, 11, 13, 21}
- arr2[] = { 11, 3, 7, 1 }, 11 is present in arr1[] = { 1, 3, 7, 11, 13, 21}
- arr2[] = { 11, 3, 7, 1 }, 11 is present in arr1[] = { 1, 3, 7, 11, 13, 21}
As all the elements are found we can conclude arr2[] is the subset of arr1[].
The algorithm is rather easy to understand. - The first array, arr1, is sorted.
- Search sorted arr1[ for the components of arr2[].
- The code will be terminated if we come across a specific value that is present in arr2[] but not in arr1[; arr2[] can never be the subset of arr1[].
- If not, arr1[ is a subset of arr2[].
The above method is implemented with the following code:
arr2[] is subset of arr1[]
## Utilize sorting and merging to determine if one array is a subset of another array.The plan is to sort the two arrays before utilising two pointers to iteratively search the second array for identical values from the first array. Every time we come across the same values, we increase the value of the pointer pointing to both arrays, and if we come across any values that are lower than those of the second array, we increase the value of the pointer pointing to the first array. We know the second array is not a subset of the first array if the value is higher than that of the second array. ## Illustration
The two arrays will first need to be sorted. - A pair of pointers, j and I or arr1[] and arr2[], should be set.
- In this case, we shall raise j by 1 if arr1[j] arr2[i].
- Increase j and I by 1 if arr1[j] = arr2[i].
- We will stop because arr2[] is not a subset of arr1[] if arr1[j] > arr2[i].
The application of the aforementioned strategy is seen below:
arr2[] is subset of arr1[]
## Utilize hashing to determine whether an array is a subset of another array.The concept is to place every element from the first array into a HashSet, iterate through the second array to see if the element is there, and if not, determine whether the second array is a subset of the first array. ## Illustration:Given array arr1[] = { 11, 1, 13, 21, 3, 7 } and arr2[] = { 11, 3, 7, 1 }. Step 1: We will store the array arr1[] elements in HashSet Step 2: We will look for each element in arr2[] in arr1[] using binary search. - arr2[] = { 11, 3, 7, 1 }, 11 is present in the HashSet = { 1, 3, 7, 11, 13, 21}
- arr2[] = { 11, 3, 7, 1 }, 3 is present in the HashSet = { 1, 3, 7, 11, 13, 21}
- arr2[] = { 11, 3, 7, 1 }, 7 is present in the HashSet = { 1, 3, 7, 11, 13, 21}
- arr2[] = { 11, 3, 7, 1 }, 1 is present in the HashSet = { 1, 3, 7, 11, 13, 21}
As all the elements are found we can conclude arr2[] is the subset of arr1[].
The algorithm is rather easy to understand. - In a HashSet, save the first array arr1[].
- Search the HashSet for the elements of arr2[].
- The code will exit if we come across a certain value that is present in arr2[] but not in the HashSet since arr2[] can never be a subset of arr1[.
- If not, arr1[ is a subset of arr2[].
The application of the aforementioned strategy is seen below:
arr2[] is subset of arr1[]
## Using Set, determine whether one array is a subset of another array.The goal is to include every member from both the first and second arrays in the set; if the set's size is the same as arr1[], then arr2[] is a subset of arr1[. Since arr2[] contains no newly discovered elements, the subset is. ## Illustration:Given array arr1[] = { 11, 1, 13, 21, 3, 7 } and arr2[] = { 11, 3, 7, 1 }. Step 1: We will store the array arr1[] and arr2[] elements in Set - The final Set = { 1, 3, 7, 11, 13, 21}
Step 2: Size of arr1[] = 6 and size of the Set = 6 - Hence no new elements are found in arr2[]
As all the elements are found we can conclude arr2[] is the subset of arr1[].
The algorithm is rather easy to understand. - Set up the first array, arr1[].
- The first array arr1[] should be kept in the same Set.
- Arr2[] is the subset of arr1[] if the size of arr1[] equals the size of the Set.
- If not, arr1[] and arr2[] are not its subset.
arr2 [] is subset of arr1 [] Given array arr1 [] = { 11, 1, 13, 21, 3, 7 } and arr2 [] = { 11, 3, 7, 1 }. Step 1: We will store the array arr1[] elements frequency in the frequency array - The frequency array will look like this
Step 2: We will look for arr2[] elements in the frequnecy array. - arr2[] = { 11, 3, 7, 1 }, 11 is present in the Frequnecy array
- arr2[] = { 11, 3, 7, 1 }, 3 is present in the Frequnecy array
- arr2[] = { 11, 3, 7, 1 }, 7 is present in the Frequnecy array
- arr2[] = { 11, 3, 7, 1 }, 1 is present in the Frequnecy array
As all the elements are found we can conclude arr2[] is the subset of arr1[].
The algorithm is rather easy to understand. - The frequency array should be used to store the frequency of the initial array elements of arr1 [].
- Search the frequency array for the elements of the arr2[] iteration.
- Reduce the frequency value by one if the value is present in the frequency array.
- We will conclude that arr2[] is not a subset of arr1[] if any element in arr2[] has a frequency of less than 1.
The application of the aforementioned strategy is seen below:
arr2[] is subset of arr1[]
Next TopicPrint a Binary Tree in Vertical Order |