Given two arrays arr1[] and arr2[]. The task is to find the count of such elements in the first array whose at-least one factor is present in the second array.

**Examples**:

Input: arr1[] = {10, 2, 13, 4, 15} ; arr2[] = {2, 4, 5, 6}Output: 4 There is no factor of 13 which is present in the second array. Except 13, factors of the rest 4 elements of the first array is present in the second array.Input: arr1[] = {11, 13, 17, 15} ; arr2[] = {3, 7, 9, 5}Output: 1

The idea is to insert all elements of the second array into a hash such that the lookup for factors can be done in constant time. Now, traverse the first array and for every element generate all of the factors starting from 1 and check if any of the factors is present in the hash or not.

Below is the implementation of the above approach:

## C++

`// CPP program to find count of ` `// elements in first arary whose ` `// atleast one factor is present ` `// in second array. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Util function to count the elements ` `// in first array whose atleast ` `// one factor is present in second array ` `int` `elementCount(` `int` `arr1[], ` `int` `n1, ` `int` `arr2[], ` `int` `n2) ` `{ ` ` ` ` ` `// counter to count number of elements ` ` ` `int` `count = 0; ` ` ` ` ` `// Hash of second array elements ` ` ` `unordered_set<` `int` `> hash; ` ` ` `for` `(` `int` `i = 0; i < n2; i++) ` ` ` `hash.insert(arr2[i]); ` ` ` ` ` `// loop to traverse through array elements ` ` ` `// and check for its factors ` ` ` `for` `(` `int` `i = 0; i < n1; i++) { ` ` ` ` ` `// generate factors of elements ` ` ` `// of first array ` ` ` `for` `(` `int` `j = 1; j * j <= arr1[i]; j++) { ` ` ` ` ` `// Check if j is a factor ` ` ` `if` `(arr1[i] % j == 0) { ` ` ` ` ` `// check if the factor is present in ` ` ` `// second array using the hash ` ` ` `if` `((hash.find(j) != hash.end()) || ` ` ` `(hash.find(arr1[i] / j) != hash.end())) { ` ` ` `count++; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr1[] = { 10, 2, 13, 4, 15 }; ` ` ` `int` `arr2[] = { 2, 4, 5, 6 }; ` ` ` ` ` `int` `n1 = ` `sizeof` `(arr1) / ` `sizeof` `(arr1[0]); ` ` ` `int` `n2 = ` `sizeof` `(arr2) / ` `sizeof` `(arr2[0]); ` ` ` ` ` `cout << elementCount(arr1, n1, arr2, n2); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find count of ` `# elements in first arary whose ` `# atleast one factor is present ` `# in second array. ` ` ` `# Importing sqrt() function ` `from` `math ` `import` `sqrt ` ` ` `# Util function to count the ` `# elements in first array ` `# whose atleast one factor is ` `# present in second array ` `def` `elementCount(arr1, arr2): ` ` ` ` ` `# counter to count ` ` ` `# number of elements ` ` ` `count ` `=` `0` ` ` ` ` `# Hash of second array elements ` ` ` `hash` `=` `frozenset` `(arr2) ` ` ` ` ` `# loop to traverse through array ` ` ` `# elements and check for its factors ` ` ` `for` `x ` `in` `arr1: ` ` ` ` ` `# generate factors of ` ` ` `# elements of first array ` ` ` `for` `j ` `in` `range` `(` `1` `, ` `int` `(sqrt(x)) ` `+` `1` `): ` ` ` ` ` `# Check if j is a factor ` ` ` `if` `x ` `%` `j ` `=` `=` `0` `: ` ` ` ` ` `# check if the factor is present ` ` ` `# in second array using the hash ` ` ` `if` `(j ` `in` `hash` `or` ` ` `x ` `/` `j ` `in` `hash` `): ` ` ` `count` `+` `=` `1` ` ` `break` ` ` ` ` `return` `count ` ` ` `# Driver code ` `arr1 ` `=` `[ ` `10` `, ` `2` `, ` `13` `, ` `4` `, ` `15` `] ` `arr2 ` `=` `[ ` `2` `, ` `4` `, ` `5` `, ` `6` `] ` ` ` `print` `(elementCount(arr1, arr2)) ` ` ` `# This code is contributed ` `# by vaibhav29498 ` |

*chevron_right*

*filter_none*

**Output**:

4

## Recommended Posts:

- For each element in 1st array count elements less than or equal to it in 2nd array | Set 2
- Count divisible pairs in an array
- Print array elements that are divisible by at-least one other
- Count number of elements between two given elements in array
- Find the element having different frequency than other array elements
- Minimum array element changes to make its elements 1 to N
- Cumulative frequency of count of each element in an unsorted array
- Unique element in an array where all elements occur k times except one
- Sum of elements in an array with frequencies greater than or equal to that element
- Check if the array has an element which is equal to sum of all the remaining elements
- Check if the array has an element which is equal to product of remaining elements
- Count distinct elements in an array
- Count elements present in first array but not in second
- Remove minimum number of elements such that no common element exist in both array
- Count of elements of an array present in every row of NxM matrix

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.