-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIfSubarray.cpp
More file actions
41 lines (38 loc) · 772 Bytes
/
IfSubarray.cpp
File metadata and controls
41 lines (38 loc) · 772 Bytes
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
#include <bits/stdc++.h>
using namespace std;
bool isSubset(int arr1[], int arr2[], int m, int n)
{
int i = 0, j = 0;
if (m < n)
return 0;
sort(arr1, arr1 + m);
sort(arr2, arr2 + n);
while (i < n && j < m)
{
if (arr1[j] < arr2[i])
j++;
else if (arr1[j] == arr2[i])
{
j++;
i++;
}
else if (arr1[j] > arr2[i])
return 0;
}
if(i < n)
return false;
else
return true;
}
int main()
{
int arr1[] = { 11, 1, 13, 21, 3, 7 };
int arr2[] = { 11, 3, 7, 1 };
int m = sizeof(arr1) / sizeof(arr1[0]);
int n = sizeof(arr2) / sizeof(arr2[0]);
if (isSubset(arr1, arr2, m, n))
printf("arr2[] is subset of arr1[] ");
else
printf("arr2[] is not a subset of arr1[] ");
return 0;
}