Find whether an array is subset of another arrayTwo arrays, arr1[0..m1] and arr2[0..n1], 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. EXAMPLESExample1 Output: arr2[] is a subset of arr1[] Example2 Output: arr2[] is a subset of arr1[] Example3 Output: 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: CODEOutput: arr2[] is subset of arr1[] Complexity of Time : O(m*n) Complexity of Space: O(1) 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.
As all the elements are found we can conclude arr2[] is the subset of arr1[]. Algorithm: The algorithm is rather easy to understand.
The above method is implemented with the following code: CODE Output: arr2[] is subset of arr1[] Complexity of Time : O(nlog (m) + mLog (m)). Sorting takes O(mLog(m)) and binary searching over every element of one array in another takes O(nlog(m)). Quick Sort is used in the code above, and its worstcase time complexity is O. (m^{2}). Complexity of Space: O(n) 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. IllustrationAlgorithm: The two arrays will first need to be sorted.
The application of the aforementioned strategy is seen below: CODE Output: arr2[] is subset of arr1[] Complexity of Time : O(mLog(m) + nLog(n)) which is better than approach 2. Complexity of Space: O(1) 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.
As all the elements are found we can conclude arr2[] is the subset of arr1[]. Algorithm: The algorithm is rather easy to understand.
The application of the aforementioned strategy is seen below: CODE Output: arr2[] is subset of arr1[] Complexity of Time : O(nLog(n)) Complexity of Space: O(n) 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
Step 2: Size of arr1[] = 6 and size of the Set = 6
As all the elements are found we can conclude arr2[] is the subset of arr1[]. Algorithm: The algorithm is rather easy to understand.
CODE Output: 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
Step 2: We will look for arr2[] elements in the frequnecy array.
As all the elements are found we can conclude arr2[] is the subset of arr1[]. Algorithm: The algorithm is rather easy to understand.
The application of the aforementioned strategy is seen below: CODE Output: arr2[] is subset of arr1[] Complexity of Time : O(m+n) which is better than methods 1,2,3 Complexity of Space: O(n) Be aware that methods 1, 2, 4, and 5 do not address the situation where we have duplicates in arr2[]. For instance, these methods will output 1, 4, 4, 2, even though it is not a subset of 1, 4, 2.
Next TopicPrint a Binary Tree in Vertical Order
