-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCheck Subset
More file actions
55 lines (48 loc) · 1.26 KB
/
Check Subset
File metadata and controls
55 lines (48 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Check Subset
Send Feedback
Given two integer arrays. Check if second array is a subset of first array.
Input format :
Line 1 : Integer N1
Line 2 : N1 integers separated be a single space
Line 1 : Integer N2
Line 2 : N2 integers separated be a single space
Output Format :
Boolean
Constraints :
0 <= N1 <= 10^4
0 <= N2 <= 10^4
Sample Input :
15
3 6 5 8 15 1 14 18 7 9 14 9 3 12 8
4
18 6 9 8
Sample Output :
true
code in java **************************************
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class solution{
public static boolean CheckSubset(int[] arr1,int n1, int[] arr2, int n2)
{
Map<Integer,Integer>map= new HashMap<>();
for(int i=0;i<n2;i++)
{
if(!map.containsKey(arr2[i]))
map.put(arr2[i],1);
else
map.put(arr2[i],map.get(arr2[i])+1);
}
for(int i=0;i<n1;i++)
{
if(map.containsKey(arr1[i]))
map.put(arr1[i],map.get(arr1[i])-1);
}
Iterator<Map.Entry<Integer,Integer>>itr=map.entrySet().iterator();
while(itr.hasNext()){
if(itr.next().getValue()>0)
return false;
}
return true;
}
}