Given an array **arr[]**, the task is to determine the number of elements of the array which are not divisible by any other element in the given array.

**Examples:**

Input:arr[] = {86, 45, 18, 4, 8, 28, 19, 33, 2}

Output:4

Explanation:

The elements are {2, 19, 33, 45} are not divisible by any other array element.

Input:arr[] = {3, 3, 3}

Output:0

**Naive Approach:** The naive approach is to iterate over the entire array and count the number of elements which are not divisible by any other elements in the given array and print the count.

Below is the implementation of the above approach:

## C++

`// CPP program for the above approach ` `#include <bits/stdc++.h> ` `#define ll long long int ` `using` `namespace` `std; ` ` ` `// Function to count the number of ` `// elements of array which are not ` `// divisible by any other element ` `// in the array arr[] ` `int` `count(` `int` `a[], ` `int` `n) ` `{ ` ` ` `int` `countElements = 0; ` ` ` ` ` `// Iterate over the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `bool` `flag = ` `true` `; ` ` ` `for` `(` `int` `j = 0; j < n; j++) { ` ` ` ` ` `// Check if the element ` ` ` `// is itself or not ` ` ` `if` `(i == j) ` ` ` `continue` `; ` ` ` ` ` `// Check for divisibility ` ` ` `if` `(a[i] % a[j] == 0) { ` ` ` `flag = ` `false` `; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `if` `(flag == ` `true` `) ` ` ` `++countElements; ` ` ` `} ` ` ` ` ` `// Return the final result ` ` ` `return` `countElements; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given array ` ` ` `int` `arr[] = { 86, 45, 18, 4, 8, ` ` ` `28, 19, 33, 2 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(` `int` `); ` ` ` ` ` `// Function Call ` ` ` `cout << count(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** *O(N ^{2})*

**Auxiliary Space:**

*O(1)*

**Efficient Approach:** To optimize the above approach, we will use the concept of Sieve of Eratosthenes. Below are the steps:

- Initialize a boolean array(say
**v[]**) of size equal to the maximum element present in the array + 1 with**true**at every index. - Traverse the given array
**arr[]**and change the value at index of multiple of current element as**false**in the array**v[]**. - Create a Hashmap and store the frequency of each element in it.
- For each element(say
**current_element**) in the array, if**v[current_element]**is true then that element is not divisible by any other element in the given array and increment the count for the current element. - Print the final value of
**count**after above steps.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count the number of ` `// elements of array which are not ` `// divisible by any other element ` `// of same array ` `int` `countEle(` `int` `a[], ` `int` `n) ` `{ ` ` ` `// Length for boolean array ` ` ` `int` `len = 0; ` ` ` ` ` `// Hash map for storing the ` ` ` `// element and it's frequency ` ` ` `unordered_map<` `int` `, ` `int` `> hmap; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// Update the maximum element ` ` ` `len = max(len, a[i]); ` ` ` `hmap[a[i]]++; ` ` ` `} ` ` ` ` ` `// Boolean array of size ` ` ` `// of the max element + 1 ` ` ` `bool` `v[len + 1]; ` ` ` ` ` `for` `(` `int` `i = 0; i <= len; i++) { ` ` ` `v[i] = ` `true` `; ` ` ` `} ` ` ` ` ` `// Marking the multiples as false ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `if` `(v[a[i]] == ` `false` `) ` ` ` `continue` `; ` ` ` ` ` `for` `(` `int` `j = 2 * a[i]; ` ` ` `j <= Glen; j += a[i]) { ` ` ` `v[j] = ` `false` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// To store the final count ` ` ` `int` `count = 0; ` ` ` ` ` `// Traverse boolean array ` ` ` `for` `(` `int` `i = 1; i <= len; i++) { ` ` ` ` ` `// Check if i is not divisible by ` ` ` `// any other array elements and ` ` ` `// appears in the array only once ` ` ` `if` `(v[i] == ` `true` ` ` `&& hmap.count(i) == 1 ` ` ` `&& hmap[i] == 1) { ` ` ` `count += 1; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the final Count ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given array ` ` ` `int` `arr[] = { 86, 45, 18, 4, 8, ` ` ` `28, 19, 33, 2 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(` `int` `); ` ` ` ` ` `// Function Call ` ` ` `cout << countEle(a, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** *O(N*log(M))* where N is the number of elements in the given array and M is the maximum element in the given array.

**Auxiliary Space:** *O(M + N)* where N is the number of elements in the given array and M is the maximum element in the given array.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count the number of elements in an array which are divisible by k
- Count elements that are divisible by at-least one element in another array
- Count of pairs of Array elements which are divisible by K when concatenated
- Count numbers in a range that are divisible by all array elements
- Minimum elements to be added in a range so that count of elements is divisible by K
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements to be divided by 2 to make at least K elements equal
- Count of array elements which are greater than all elements on its left
- Count of array elements which is smaller than both its adjacent elements
- Count number of elements between two given elements in array
- Count array elements that divide the sum of all other elements
- Count of elements on the left which are divisible by current element
- Maximum count of elements divisible on the left for any element
- Count of elements on the left which are divisible by current element | Set 2
- Print array elements that are divisible by at-least one other
- Sum of all the elements in an array divisible by a given number K
- Maximum sum of elements divisible by K from the given array
- Product of all the elements in an array divisible by a given number K
- Permutation of Array such that sum of adjacent elements are not divisible by 3
- Make all array elements divisible by a number K

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.