Array Subset of another array Python
PROGRAM TO FIND WHETHER AN ARRAY IS SUBSET OF ANOTHER ARRAY USING MAP
OUTPUT
arr2[] is subset of arr1[]
# Function to check if an array is # subset of another array def isSubset(a, b, m, n) : # map to store the values of array a mp1 = {} for i in range(m): if a[i] not in mp1: mp1[a[i]] = 0 mp1[a[i]] += 1 # flag value f = 0 for i in range(n): # if b[i] is not present in map # then array b can not be a # subset of array a if b[i] not in mp1: f = 1 break # if if b[i] is present in map # decrement by one else : mp1[b[i]] -= 1 if (mp1[b[i]] == 0): mp1.pop(b[i]) return f # Driver code arr1 = [11, 1, 13, 21, 3, 7 ] arr2 = [11, 3, 7, 1 ] m = len(arr1) n = len(arr2) if (not isSubset(arr1, arr2, m, n)): print("arr2[] is subset of arr1[] ") else: print("arr2[] is not a subset of arr1[]")
arr2[] is subset of arr1[]
Comments
Post a Comment