10) Given two arrays of positive integers, output the largest number in the first array not present in the
second one.
Input
The first line contains the size of the first array.
The second line contains the contents of first array.
The third line contains the size of the second array.
The fourth line contains the contents of second array.
Output
Output the smallest number occurring in the first array that does not occur in the second.
In case there is no such number, output 0.
Note : The sizes of the arrays are smaller than 20.
Example
Input:
3
2 3 4
4
1 4 5 2
Output: 3
#include
#include
#define MAX_SIZE 20
int largest_not_in_second(int n, int arr1[], int m, int arr2[]) {
int i, j, result = 0;
int first_set[MAX_SIZE] = {0};
int second_set[MAX_SIZE] = {0};
for (i = 0; i < n; i++) {
first_set[arr1[i]] = 1;
}
for (i = 0; i < m; i++) {
second_set[arr2[i]] = 1;
}
for (i = 0; i < MAX_SIZE; i++) {
if (first_set[i] == 1 && second_set[i] == 0) {
if (result == 0) {
result = i;
} else {
result = result < i ? result : i;
}
}
}
return result;
}
int main() {
int n, m, i;
int arr1[MAX_SIZE];
int arr2[MAX_SIZE];
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &arr1[i]);
}
scanf("%d", &m);
for (i = 0; i < m; i++) {
scanf("%d", &arr2[i]);
}
printf("%d\n", largest_not_in_second(n, arr1, m, arr2));
return 0;
}