An array is a simple data structure that stores a collection of elements. Each element in the array is referenced by its unique identifier using an index to the contiguous memory locations where the elements are stored.
Note: To learn more about arrays, click here
Consider array1
and array2
are two integer arrays of size m and n, respectively (n <= m). We need to determine whether array2
is a subset of array1
or not. A subset of an array is one in which every element of array2
is present within array1
. Assume that, in neither of the arrays, any elements are repeated.
This solution is known as the Brute force approach using two loops. Using a linear search, look for each element of array2
in array1
. The array2
is a subset of array1
only if all of its elements are present in array1
.
# Function to find subset in arraydef subset(array1, array2, size1, size2):i = 0for i in range(size2):j=0while(j < size1):if(array2[i] == array1[j]):break #if broken then element is presentj=j+1if (j == size1):return False#return true as all elements of arr2[] are present in arr1[]return True#driver codearray1 = [20, 12, 13, 21, 5, 8]array2 = [20, 5, 8, 12]size1 = len(array1)size2 = len(array2)if(subset(array1, array2, size1, size2)):print("Array2[] is subset of Array1[] ")else:print("array2[] is not a subset of array1[]")
Lines 1–6: We define the subset
function in which we initialize the loop variables, i
and j
. We create two loops, an inner and outer loop.
Lines 7–10: If we find the same element in both arrays, break the inner loop.
Lines 10–13: We return False
if j
gets equal to the size1
. Otherwise, we return True
.
Time complexity: For this approach, the time complexity is
Space complexity: For this approach, the space complexity is
In this method, we sort both array1
and array2
arrays, which helps in reducing the time complexity compared to the aforementioned method.
To verify that each element in sorted array2
is also present in sorted array1
, we perform a merge type of process.
def subset(array1, array2, size1, size2):i = 0j = 0if size1 < size2: #subset not existreturn False#sorting both arraysarray1.sort()array2.sort()while i < size2 and j < size1: #traversing arraysif array1[j] < array2[i]:j += 1elif array1[j] == array2[i]: #if equal, go to next index j += 1i += 1elif array1[j] > array2[i]: #if array1 element is greaterreturn Falsereturn False if i < size2 else True# Driver codearray1 = [20, 12, 13, 21, 5, 8]array2 = [20, 5, 8, 12]size1 = len(array1)size2 = len(array2)if subset(array1, array2, size1, size2) == True:print("Array2 is subset of Array1 ")else:print("Array2 is not a subset of Array1 ")
Lines 1–5: We define the function subset
in which we initialize loop variables i
and j
. We return False
if size1
is smaller than size2
.
Lines 6–14: We sort both arrays and in while
loop. We check whether the current element of array1
is less than or equal to the array2
. If yes, increment the loop variable.
Lines 15–17: We return False
if the current element of array1
is greater and True
if size1
is greater than i
.
Time complexity: For this approach, the time complexity is
Space complexity: For this approach, the space complexity is
Free Resources