-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathsubset_of_array.cpp
48 lines (46 loc) · 1.08 KB
/
subset_of_array.cpp
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
/*
Given two arrays: arr1[0..m-1] of size m and arr2[0..n-1] of size n. Task is to check whether arr2[] is a subset
of arr1[] or not. Both the arrays can be both unsorted or sorted. It may be assumed that elements in both array are distinct.
*/
#include <iostream>
#include <unordered_set>
using namespace std;
bool isSubset(int arr1[], int arr2[], int m, int n)
{
unordered_set<int> s;
for (int index = 0; index < m; index++)
{
s.insert(arr1[index]);
}
for (int index = 0; index < n; index++)
{
if (s.find(arr2[index]) == s.end())
return false;
}
return true;
}
int main()
{
//code
int t;
cin >> t;
while (t--)
{
int m, n, flag = 0;
cin >> m >> n;
int arr1[m], arr2[n];
for (int index = 0; index < m; index++)
{
cin >> arr1[index];
}
for (int index = 0; index < n; index++)
{
cin >> arr2[index];
}
if (isSubset(arr1, arr2, m, n))
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}