Elements of an array that are not divisible by any element of another array in C++


In this problem, we are given two arrays arr1[] and arr2[]. Our task is to create a program to find the elements of an array that are not divisible by any element of another array. 

Problem Description: Here, we need to find all elements from arr1 that are not divisible by any elements of arr2.

Let’s take an example to understand the problem,

Input: arr1[] = {17, 15, 5, 12, 8}        arr2[] = {5, 4}

Output: 17

Explanation −

Elements of arr1 and elements dividing them,

17 -> no element can divide it.
15 -> 5 divides the element.
5 -> 5 divides the element.

12 -> 4 divides the element.

8 -> 4 divides the element.

Solution Approach −

A simple and naive approach to solve the problem is by using the direct method. We will loop through arr1 and for each element of arr1, we will check if any element of arr2 divides the element. If no element divides it, print the element.

Algorithm −

Step 1: loop for arr1, i -> 0 to n-1.

Step 2.1: for each arr1[i], loop arr2, for j -> 0 to n-1.

Step 2.2.1 : if arr1[i] % arr2[j] == 0, continue flag = -1.

Step 2.3: if flag != -1, print arr1[i].

Program to illustrate the working of our solution,

Example

Live Demo

#include<iostream>
using namespace std;

void findEleNotDivisbleByArray(int arr1[], int arr2[], int arr1Size, int arr2Size) {
   
   int flag = 0;
   for (int i = 0; i < arr1Size; i++) {
      flag = 0;
      for (int j = 0; j < arr2Size; j++){
         
         if( arr1[i] % arr2[j] == 0 ) {
            flag = -1;   
            break;
         }
      }
      if ( flag == 0 )
            cout<<arr1[i]<<"\t";
   }
}

int main()
{
   int arr1[] = {17, 15, 5, 12, 23, 8};
   int arr2[] = {5, 4};
   int arr1Size = sizeof(arr1)/sizeof(arr1[0]);
   int arr2Size = sizeof(arr2)/sizeof(arr2[0]);
   cout<<"Elements of an array that are not divisible by any element of another array are ";
   findEleNotDivisbleByArray(arr1, arr2, arr1Size, arr2Size);
   return 0;
}

Output −

Elements of an array that are not divisible by any element of another array are 17 23

This solution is valid but is not efficient. So, let’s see an efficient solution to the problem,

In this method, we will create an array isDivisible[] of elements of arr1. For all elements of arr2, mark all their multiples till the largest element of arr1. And print all elements that are marked false in isDisible array.

Program to illustrate the working of our solution,

Example

Live Demo

#include<iostream>
using namespace std;

void findEleNotDivisbleByArray(int arr1[], int arr2[], int arr1Size, int arr2Size) {
   
   int maxEle = 0;
   for (int i = 0; i < arr1Size; i++)
      if (arr1[i] > maxEle)
         maxEle = arr1[i];

   int mark[maxEle];
   for (int i = 0; i < arr2Size; i++)
      for (int j = arr2[i]; j <= maxEle; j += arr2[i])
         mark[j] = 1;
   for (int i = 0; i < arr1Size; i++)
      if ( mark[arr1[i]] != 1)
         cout << arr1[i] << endl;
}

int main()
{
   int arr1[] = {17, 15, 5, 12, 8};
   int arr2[] = {5, 4};
   int arr1Size = sizeof(arr1)/sizeof(arr1[0]);
   int arr2Size = sizeof(arr2)/sizeof(arr2[0]);
   cout<<"Elements of an array that are not divisible by any element of another array are ";
   findEleNotDivisbleByArray(arr1, arr2, arr1Size, arr2Size);
   return 0;
}

Output −

Elements of an array that are not divisible by any element of another array are 17

Updated on: 22-Jan-2021

326 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements