Count of elements not divisible by any other elements of Array
Last Updated :
24 May, 2021
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 that are not divisible by any other elements in the given array and print the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int count( int a[], int n)
{
int countElements = 0;
for ( int i = 0; i < n; i++) {
bool flag = true ;
for ( int j = 0; j < n; j++) {
if (i == j)
continue ;
if (a[i] % a[j] == 0) {
flag = false ;
break ;
}
}
if (flag == true )
++countElements;
}
return countElements;
}
int main()
{
int arr[] = { 86, 45, 18, 4, 8,
28, 19, 33, 2 };
int n = sizeof (arr) / sizeof ( int );
cout << count(arr, n);
return 0;
}
|
Java
class GFG{
static int count( int a[], int n)
{
int countElements = 0 ;
for ( int i = 0 ; i < n; i++)
{
boolean flag = true ;
for ( int j = 0 ; j < n; j++)
{
if (i == j)
continue ;
if (a[i] % a[j] == 0 )
{
flag = false ;
break ;
}
}
if (flag == true )
++countElements;
}
return countElements;
}
public static void main(String[] args)
{
int arr[] = { 86 , 45 , 18 , 4 , 8 ,
28 , 19 , 33 , 2 };
int n = arr.length;
System.out.print(count(arr, n));
}
}
|
Python3
def count(a, n):
countElements = 0
for i in range (n):
flag = True
for j in range (n):
if (i = = j):
continue
if (a[i] % a[j] = = 0 ):
flag = False
break
if (flag = = True ):
countElements + = 1
return countElements
if __name__ = = "__main__" :
arr = [ 86 , 45 , 18 , 4 ,
8 , 28 , 19 , 33 , 2 ]
n = len (arr)
print ( count(arr, n))
|
C#
using System;
class GFG{
static int count( int []a,
int n)
{
int countElements = 0;
for ( int i = 0; i < n; i++)
{
bool flag = true ;
for ( int j = 0; j < n; j++)
{
if (i == j)
continue ;
if (a[i] % a[j] == 0)
{
flag = false ;
break ;
}
}
if (flag == true )
++countElements;
}
return countElements;
}
public static void Main(String[] args)
{
int []arr = {86, 45, 18, 4, 8,
28, 19, 33, 2};
int n = arr.Length;
Console.Write(count(arr, n));
}
}
|
Javascript
<script>
function count(a, n)
{
let countElements = 0;
for (let i = 0; i < n; i++) {
let flag = true ;
for (let j = 0; j < n; j++) {
if (i == j)
continue ;
if (a[i] % a[j] == 0) {
flag = false ;
break ;
}
}
if (flag == true )
++countElements;
}
return countElements;
}
let arr = [ 86, 45, 18, 4, 8,
28, 19, 33, 2 ];
let n = arr.length;
document.write(count(arr, n));
</script>
|
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 the index of multiple of current elements 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 the above steps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countEle( int a[], int n)
{
int len = 0;
unordered_map< int , int > hmap;
for ( int i = 0; i < n; i++) {
len = max(len, a[i]);
hmap[a[i]]++;
}
bool v[len + 1];
for ( int i = 0; i <= len; i++) {
v[i] = true ;
}
for ( int i = 0; i < n; i++) {
if (v[a[i]] == false )
continue ;
for ( int j = 2 * a[i];
j <= len; j += a[i]) {
v[j] = false ;
}
}
int count = 0;
for ( int i = 1; i <= len; i++) {
if (v[i] == true
&& hmap.count(i) == 1
&& hmap[i] == 1) {
count += 1;
}
}
return count;
}
int main()
{
int arr[] = { 86, 45, 18, 4, 8,
28, 19, 33, 2 };
int n = sizeof (arr) / sizeof ( int );
cout << countEle(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countEle( int a[],
int n)
{
int len = 0 ;
HashMap<Integer,
Integer> hmap = new HashMap<>();
for ( int i = 0 ; i < n; i++)
{
len = Math.max(len, a[i]);
if (hmap.containsKey(a[i]))
{
hmap.put(a[i],
hmap.get(a[i]) + 1 );
}
else
{
hmap.put(a[i], 1 );
}
}
boolean []v = new boolean [len + 1 ];
for ( int i = 0 ; i <= len; i++)
{
v[i] = true ;
}
for ( int i = 0 ; i < n; i++)
{
if (v[a[i]] == false )
continue ;
for ( int j = 2 * a[i];
j <= len; j += a[i])
{
v[j] = false ;
}
}
int count = 0 ;
for ( int i = 1 ; i <= len; i++)
{
if (v[i] == true &&
hmap.containsKey(i) &&
hmap.get(i) == 1 &&
hmap.get(i) == 1 )
{
count += 1 ;
}
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 86 , 45 , 18 , 4 , 8 ,
28 , 19 , 33 , 2 };
int n = arr.length;
System.out.print(countEle(arr, n));
}
}
|
Python3
def countEle(a, n):
len = 0
hmap = {}
for i in range (n):
len = max ( len , a[i])
hmap[a[i]] = hmap.get(a[i], 0 ) + 1
v = [ True for i in range ( len + 1 )]
for i in range (n):
if (v[a[i]] = = False ):
continue
for j in range ( 2 * a[i], len + 1 , a[i]):
v[j] = False
count = 0
for i in range ( 1 , len + 1 ):
if (v[i] = = True and (i in hmap) and hmap[i] = = 1 ):
count + = 1
return count
if __name__ = = '__main__' :
arr = [ 86 , 45 , 18 , 4 , 8 , 28 , 19 , 33 , 2 ]
n = len (arr)
print (countEle(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int countEle( int []a,
int n)
{
int len = 0;
Dictionary< int ,
int > hmap =
new Dictionary< int ,
int >();
for ( int i = 0; i < n; i++)
{
len = Math.Max(len, a[i]);
if (hmap.ContainsKey(a[i]))
{
hmap[a[i]]++;
}
else
{
hmap.Add(a[i], 1);
}
}
bool []v = new bool [len + 1];
for ( int i = 0; i <= len; i++)
{
v[i] = true ;
}
for ( int i = 0; i < n; i++)
{
if (v[a[i]] == false )
continue ;
for ( int j = 2 * a[i];
j <= len; j += a[i])
{
v[j] = false ;
}
}
int count = 0;
for ( int i = 1; i <= len; i++)
{
if (v[i] == true &&
hmap.ContainsKey(i) &&
hmap[i] == 1 &&
hmap[i] == 1)
{
count += 1;
}
}
return count;
}
public static void Main(String[] args)
{
int []arr = {86, 45, 18, 4, 8,
28, 19, 33, 2};
int n = arr.Length;
Console.Write(countEle(arr, n));
}
}
|
Javascript
<script>
function countEle(a, n)
{
let len = 0;
let hmap = new Map();
for (let i = 0; i < n; i++)
{
len = Math.max(len, a[i]);
if (hmap.has(a[i]))
{
hmap.set(a[i],
hmap.get(a[i]) + 1);
}
else
{
hmap.set(a[i], 1);
}
}
let v = Array.from({length: len + 1}, (_, i) => 0);
for (let i = 0; i <= len; i++)
{
v[i] = true ;
}
for (let i = 0; i < n; i++)
{
if (v[a[i]] == false )
continue ;
for (let j = 2 * a[i];
j <= len; j += a[i])
{
v[j] = false ;
}
}
let count = 0;
for (let i = 1; i <= len; i++)
{
if (v[i] == true &&
hmap.has(i) &&
hmap.get(i) == 1 &&
hmap.get(i) == 1)
{
count += 1;
}
}
return count;
}
let arr = [86, 45, 18, 4, 8,
28, 19, 33, 2];
let n = arr.length;
document.write(countEle(arr, n));
</script>
|
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.
Share your thoughts in the comments
Please Login to comment...