Array Subset of another array | Practice | GeeksforGeeks

Given two arrays: a1[0..n-1] of size n and a2[0..m-1] of size m. Task is to check whether a2[] is a subset of a1[] or not. Both the arrays can be sorted or unsorted. 
 
Example 1:
Input:
a1[] = {11, 1, 13, 21, 3, 7}
a2[] = {11, 3, 7, 1}
Output:
Yes
Explanation:
a2[] is a subset of a1[]

Example 2:
Input:
a1[] = {1, 2, 3, 4, 5, 6}
a2[] = {1, 2, 4}
Output:
Yes
Explanation:
a2[] is a subset of a1[]

Example 3:
Input:
a1[] = {10, 5, 2, 23, 19}
a2[] = {19, 5, 3}
Output:
No
Explanation:
a2[] is not a subset of a1[]
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function isSubset() which takes the array a1[], a2[], its size n and m as inputs and return "Yes" if arr2 is subset of arr1 else return "No" if arr2 is not subset of arr1.
 
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n)


Constraints:
1 <= n,m <= 105
1 <= a1[i], a2[j] <= 105



 
 
0
kvkhairkarin 8 hours
#Your Code here 
s1 = set(a1)   s2 = set(a2)   if s2.intersection(s1) == s2:       return "Yes"   else:       return "No"
0
ayckerayush3 hours ago
 check this out (c++)
sort(a1,a1+n);
    sort(a2,a2+m);
    int indexA1=0;
    int indexA2=0;
    if(m>n){
        return "No";
    }
    for(int i=0;i<m;i++){
        for(int j=indexA1;j<n;j++){
            if(a1[j]==a2[i]){
                indexA1=j+1;
                break;
            }
            else if(a1[j]>a2[i] || j>=n-1){
                return "No";
            }
        }
    }
    return "Yes";
0
surabhichoubey551 day ago
string isSubset(int a1[], int a2[], int n, int m) {      //make unordered_set which will store all array1 elements   unordered_set<int>s1;   //array1 elements insert     for(int i = 0;i<n;i++)     {         s1.insert(a1[i]);     }   //now treverse array2    for(int i = 0;i<m;i++)    {        if(s1.find(a2[i]) == s1.end())        {            return "No";        }    }   //also while treverseing cheack if array element is not seen in array1 return false      return "Yes";   //else return true or yes         }
0
aamir255y2 days ago
string isSubset(int a1[], int a2[], int n, int m) {
    unordered_map<int, int> map_a1;
    
    for(int i = 0; i < n; ++i) map_a1[ a1[i] ] = 1;
    
    for(int i = 0; i < m; ++i) {
        if( map_a1[ a2[i] ] != 1 )
            return "No";
    }
    return "Yes";
}
0
abhishekjune20162 days ago
map<int,int>mp;   for(int i=0;i<n;i++)   {       mp[a1[i]]++;   }   // for (auto i : mp)   //     cout << i.first << "   " << i.second   //          << endl;   map<int, int>::iterator it ;   int count=0;   for(int i=0;i<m;i++)   {       it=mp.find(a2[i]);       if(it!=mp.end())       {           count++;       }   }   // cout<<count<<endl;   if(count==m)   {       return "Yes";   }   else   {       return "No";   }
0
abhishekjune2016
This comment was deleted.
0
arobindosuklabaidya4 days ago
STL Solution !!!!!
 
string isSubset(int a1[], int a2[], int n, int m) {      unordered_map<int,int> mp;   for(int i=0;i<n;i++){       mp[a1[i]]++;   }      for(int i=0;i<m;i++){       if(mp[a2[i]]==0){           return "No";       }   }   return "Yes";   }
0
sankeerthsirikonda5 days ago
#python
def isSubset( a1, a2, n, m):   for i in a2:       if i not in a1:           return 'No'           break   return 'Yes'
+1
hayatunisha15 days ago
//We will store the intersection of a1 and a2 into ans
//if all the elements of ans are same as a2 return yes else return no
string isSubset(int a1[], int a2[], int n, int m) {   int ans[m];   int i,j,k;   i=j=k=0;      sort(a1,a1+n);   sort(a2,a2+m);      while(i<n&&j<m){       if(a1[i]<a2[j]){i++;}       else if(a1[i]>a2[j]){j++;}       else if(a1[i]==a2[j]){ans[k++]=a1[i++]; j++;}   }      for(int i = 0;i<m;i++){       if(ans[i]!=a2[i]){return "No";}   }   return "Yes";}
+1
imjunior4716 days ago
        HashSet<Long> set = new HashSet<>();       for(long ele : a1) set.add(ele);       for(long ele : a2){           if(!set.contains(ele)) return "No";       }       return "Yes";
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.