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. It may be assumed that elements in both array are distinct.
 
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
kuldeepahirwarmba3 hours ago
 public String isSubset( long a1[], long a2[], long n, long m) {       long len = Math.min(n,m);       long max = Math.max(n,m);      boolean result = false;       for(int i=0;i<len;i++)       {           for(int j=0;j<max;j++)           {               if(a2.length<=a1.length)               {               if(a2[i]==a1[j])               {                   result = true;                   break;               }else{                   result = false;               }               }           }           if(result == false)           {               break;           }       }       if(result)       {           return "Yes";       }else{           return "No";       }
0
pmisrah31051 day ago
Solution using Maps:
string isSubset(int a1[], int a2[], int n, int m) { string str=""; bool ans=true; unordered_map<int,int> mp; for(int i=0;i<n;i++){     mp[a1[i]]+=1;      } for(int i=0;i<m;i++){     if(mp.count(a2[i])>0){         ans=true;     }     else{        ans=false;        break;     }          } if(ans){     str+="Yes"; } else{     str+="No"; } return str;}
0
ashrithbhgowda2 days ago
 
//for c++ 
  unordered_set<int> s;   for(int i=0;i<n;i++){       s.insert(a1[i]);   }   bool found=false;   for(int i=0;i<m;i++){       if(s.find(a2[i])!=s.end()){           found=true;       }       else{           found=false;           break;       }   }   if(found==true){       return "Yes";   }   else{       return "No";   }
0
anshitasinha3 days ago
PYTHON SOLUTION:
   def isSubset( a1, a2, n, m):   count=0   for i in a2:       if i in a1:           count+=1   if count==len(a2):       return "Yes"   return "No"
0
anigambe203 days ago
string isSubset(int a1[], int a2[], int n, int m) {   map<int,int> ma;   int cnt=0;   for(int i=0; i<n; i++)   {       ma[a1[i]]++;   }   for(int i=0; i<m; i++)   {       if(ma[a2[i]])       {           cnt++;       }   }   if(cnt!=m)   {       return "No";   }   else   {       return "Yes";   }   }
0
minipandey3325 days ago
string isSubset(int a1[], int a2[], int n, int m) {      map <int,int> m1;      for(int i=0; i<n; i++)   {       m1[a1[i]]++; //    }      for(int j=0; j<m; j++)   {       m1[a2[j]]++;              if(m1[a2[j]] <= 1)       return "No";   }   return "Yes";      }
0
bhavya0381cse196 days ago
class Compute {   public String isSubset( long a1[], long a2[], long n, long m) {              Set <Long> set=new HashSet<>();       for(int i=0;i<n;i++)     {         set.add(a1[i]);     }     int flag=0;     for(int i=0;i<m;i++)     {         if(!set.contains(a2[i]))         {             flag=1;            break;         }           }       if(flag==0)       {           return "Yes";       }       else       {           return "No";       }         }}
0
blaze_061 week ago
Just check if the elements of a2 are in a1 if they are then simply delete it from a2 costing constant time.
Later on check if the size of a2 is larger than 0, if it is then you know a2 aint a subset of a1.
 
python- 0.1sec (i am sure it will be faster in other languages)
0
pawancoding00111 week ago
string isSubset(int a1[], int a2[], int n, int m) {   unordered_set<int>s(a1,a1+n);   for(int i=0;i<m;i++)   {       if(s.find(a2[i])==s.end())return "No";          }   return "Yes";   }
0
nitishkamath635902 weeks ago
// c++ solution
string isSubset(int a1[], int a2[], int n, int m) {      // use unordered_map(ump) to improve time complexity   unordered_map<int,int> ump;   // accessing each element and putting it into unordered map   int i =0;   while(i<n)   {       ump[a1[i]]=1;       i++;   }      // this is checking condition    // if element found in ump then its freq must be 1 bcz given that element is unique   int j = 0;   while(j<m)   {       // if freq != 1 -> it means that freq is zero -> element is not present in master array-> return No       if(ump[a2[j]]!=1)       {              return "No";       }       j++;   }   // if all element is present in ump then we will print yes   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.