Count elements that are divisible by at-least one element in another array in C++

C++Server Side ProgrammingProgramming

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.

For Example

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.

Approach used in the below program is as follows

  • 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.

Example

 Live Demo

#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;
}

Output

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

count is 2
raja
Published on 15-May-2020 15:18:24
Advertisements