- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given two arrays, let's say arr_1[] and arr_2[] both containing integer values and the task is to calculate the count of elements that are divisible by at least one element in another array. It means there we need to count those elements having at least one factor in the second array which is arr_2.

Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Input− int arr_1[] = {1, 2, 3, 4, 5} arr_2[] = {2, 6, 12, 15}Output− count is 2

**Explanation** − There are 5 elements in arr_1[] and 4 elements in arr_2[]. All the elements in arr_1[] are divisible by arr_2[]. So the count is 5.

Input− int arr_1[] = {1, 2, 3, 4, 5} arr_2[] = {13, 11}Output− count is 0

**Explanation** − There are 5 elements in arr_1[] and 2 elements in arr_2[]. None ofl the elements in arr_1[] are divisible by arr_2[]. So the count is 0.

Create two arrays let’s say, arr_1[] and arr_2[]

Calculate the length of both the arrays using the length() function that will return an integer value as per the elements in an array.

Take a temporary variable that will store the count of elements.

Create an unordered_set variable let’s say us

Start loop for i to 0 and i less than size of second array.

Inside the loop perform insert in arr_2[i].

Start another loop for i to 0 and i less than the size of the first array.

Inside the loop, start another loop for j to and j * j < = arr_1[i]

Inside this check if arr_1[i]%j = 0 then check if us.find(j)!=us.end OR us.find(arr[i]/j) != us.end() then increment the count by 1

Else,, break

Return count

Print the result.

#include <iostream> #include <unordered_set> using namespace std; // Function to count the number of elements // in first array whose atleast one factor is // present in the second array int totalelements(int arr_1[], int size1, int arr_2[], int size2){ // variable 'result' to count the number of elements int result = 0; // Hash of second array elements unordered_set<int> h; for (int i = 0; i < size2; i++){ h.insert(arr_2[i]); } // traverse through array elements // and find its factors for (int i = 0; i < size1; i++){ for (int j = 1; j * j <= arr_1[i]; j++){ if (arr_1[i] % j == 0){ // check if the factor is present in // second array using the h if ((h.find(j) != h.end()) || (h.find(arr_1[i] / j)!= h.end())){ result++; break; } } } } return result; } // Main function int main(){ int arr_1[] = { 1, 2, 3, 4, 5 }; int arr_2[] = { 2, 6, 12, 15 }; int size1 = sizeof(arr_1) / sizeof(arr_1[0]); int size2 = sizeof(arr_2) / sizeof(arr_2[0]); cout <<"count is "<<totalelements(arr_1, size1, arr_2, size2); return 0; }

If we run the above code we will get the following output −

count is 2

- Related Questions & Answers
- Print array elements that are divisible by at-least one other in C++
- Elements of an array that are not divisible by any element of another array in C++
- Count pairs in an array such that at least one element is prime in C++
- Count numbers in a range that are divisible by all array elements in C++
- Find an array element such that all elements are divisible by it using c++
- Maximum value K such that array has at-least K elements that are >= K in C++
- Count pairs in an array such that frequency of one is at least value of other in C++
- Count the number of elements in an array which are divisible by k in C++
- Program to find minimum sum subsequence by taking at least one element from consecutive 3 elements in python
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times in C++
- Count divisors of n that have at-least one digit common with n in Java
- Divide every element of one array by other array elements in C++ Program
- Frequency of elements of one array that appear in another array using JavaScript
- Count of arrays in which all adjacent elements are such that one of them divide the another in C++
- Count of m digit integers that are divisible by an integer n in C++

Advertisements