Count of elements not divisible by any other elements of Array

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Output:

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:

  1. Initialize a boolean array(say v[]) of size equal to the maximum element present in the array + 1 with true at every index.
  2. Traverse the given array arr[] and change the value at index of multiple of current element as false in the array v[].
  3. Create a Hashmap and store the frequency of each element in it.
  4. 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.
  5. Print the final value of count after above steps.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


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.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

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.