
- 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
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
#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
#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
- Related Questions & Answers
- Count elements that are divisible by at-least one element in another array in C++
- Find an array element such that all elements are divisible by it using c++
- Find elements of an array which are divisible by N using STL in C++
- Print array elements that are divisible by at-least one other in C++
- Count the number of elements in an array which are divisible by k in C++
- Count numbers in a range that are divisible by all array elements in C++
- Frequency of elements of one array that appear in another array using JavaScript
- C++ Permutation of an Array that has Smaller Values from Another Array
- Divide every element of one array by other array elements in C++ Program
- Product of all the elements in an array divisible by a given number K in C++
- Find the Numbers that are not divisible by any number in the range [2, 10] using C++
- Maximum possible XOR of every element in an array with another array in C++
- Get values that are not present in another array in JavaScript
- Find a non empty subset in an array of N integers such that sum of elements of subset is divisible by N in C++
- Check if LCM of array elements is divisible by a prime number or not in Python