-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathis_subset_of_array.c
80 lines (74 loc) · 1.86 KB
/
is_subset_of_array.c
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Date: 2018-10-06
*
* Description:
* Given 2 sorted arrays arr1 and arr2 having distinct elements, find if arr2
* is subset of arr1. arr2 will considered subset of arr1 if arr1 contains all
* elements of arr2.
*
* Approach:
* Scan array linearly keeping track of index of smaller and larger array.
* If current element is same increment both indexes,
* If element at arr1 is smaller increment index of first array
* Otherwise if arr1 element becomes more than of arr2, arr2 can't be subset of
* arr1.
*
* Complexity:
* O(N), N = Number of elements in longer elements.
*/
#include "stdio.h"
#include "stdlib.h"
int main() {
int i = 0, j = 0;
int n1 = 0, n2 = 0;
int *arr1 = NULL, *arr2 = NULL;
printf("Enter number of elements in first array, arr1: ");
scanf("%d", &n1);
arr1 = (int *)malloc(sizeof(int) * n1);
for (i = 0; i < n1; i++) {
printf("Enter element[%d]: ", i);
scanf("%d", &arr1[i]);
}
printf("Enter number of elements in second array, arr2: ");
scanf("%d", &n2);
arr2 = (int *)malloc(sizeof(int) * n2);
for (i = 0; i < n2; i++) {
printf("Enter element[%d]: ", i);
scanf("%d", &arr2[i]);
}
i = 0;
j = 0;
while ((i < n1) && (j < n2)) {
if (arr1[i] == arr2[j]) {
i++;
j++;
}
else if (arr1[i] < arr2[j])
i++;
else {
printf("arr2 is not subset of arr1\n");
return 0;
}
}
if (j < n2)
printf("j < n2, arr2 is not subset of arr1\n");
else
printf("arr2 is subset of arr1\n");
return 0;
}
/*
* Output:
* --------------
* Enter number of elements in first array, arr1: 6
* Enter element[0]: 1
* Enter element[1]: 2
* Enter element[2]: 3
* Enter element[3]: 4
* Enter element[4]: 5
* Enter element[5]: 6
* Enter number of elements in second array, arr2: 3
* Enter element[0]: 1
* Enter element[1]: 2
* Enter element[2]: 4
* arr2 is subset of arr1
*/