top of page

Keep Reading !

You can also checkout more topics like...

peep-101_edited.png
EXPLORE COURSES (6).png
EXPLORE COURSES (9).png
EXPLORE COURSES (7)_edited.jpg

Array Subset of another array

a1[upto n-1] of size n and a2[upto m-1] of size m. The task is to check whether a2[] is a subset of a1[] or not. Both arrays can be sorted or unsorted.

Example 01:

Input:
a1[] = {110, 10, 130, 210, 3, 7}
a2[] = {110, 3, 7, 10}
Output:
Yes
Details:
a2[] is a subset of a1[]

Example 2:

Input:
a1[] = {10, 20, 3, 40, 5, 6}
a2[] = {10, 20, 40}
Output:
Yes
Details:
a2[] is a subset of a1[]

Example 3:

Input:
a1[] = {190, 59, 29, 293, 199}
a2[] = {199, 59, 39}
Output:
No
Details:
a2[] is not a subset of a1[]

Your Task: return "Yes" if arr2 is subset of arr1 else return "No" if arr2 is not subset of arr1.

Time Complexity: O(n) Auxiliary Space: O(n) Constraints: 1 <= n,m <= 105 1 <= a1[i], a2[j] <= 106 Solution


public String isSubset( long a1[], long a2[], long n, long m) {
       HashMap<Long,Integer> map1 = new HashMap<>();
       long count = m;
       for(int i = 0; i<n; i++)
       {
        if(map1.containsKey(a1[i]))
               {
               int value =  map1.get(a1[i]);
               map1.put(a1[i],value+1);
                 }else
                {
                map1.put(a1[i],1);
                       }
                     }
       for(int i = 0; i<m; i++)
           {
                  if(map1.containsKey(a2[i]))
                     {
                count--;
              if(map1.get(a2[i]) >0)
                     {
                       int value = map1.get(a2[i]);
                    map1.put(a2[i],value-1);
                            }
                     }
              }
       if(count == 0)
       {
       return "Yes";
       }
       return "No";
       }


128 views0 comments

Recent Posts

See All

Comments


bottom of page