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; } |
4
Time Complexity: O(N2)
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; } |
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:
- Elements of an array that are not divisible by any element of another array
- Count of lists which are not a subset of any other given lists
- Check if any permutation of array contains sum of every adjacent pair not divisible by 3
- Print array elements that are divisible by at-least one other
- Maximum count of elements divisible on the left for any element
- Check if any permutation of a number without any leading zeros is a power of 2 or not
- Check if any large number is divisible by 19 or not
- Check if any large number is divisible by 17 or not
- Numbers that are not divisible by any number in the range [2, 10]
- Find a N-digit number such that it is not divisible by any of its digits
- Find a N-digit number such that it is not divisible by any of its digits
- Find permutation of n which is divisible by 3 but not divisible by 6
- Find set of m-elements with difference of any two elements is divisible by k
- Count array elements that divide the sum of all other elements
- Largest possible Subset from an Array such that no element is K times any other element in the Subset
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Find original array from encrypted array (An array of sums of other elements)
- Choose two elements from the given array such that their sum is not present in any of the arrays
- Check if LCM of array elements is divisible by a prime number or not
- Permutation of Array such that sum of adjacent elements are not divisible by 3
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.

