Count numbers from a given range that are not divisible by any of the array elements
Last Updated :
11 May, 2021
Given an array arr[] consisting of N positive integers and integers L and R, the task is to find the count of numbers in the range [L, R] which are not divisible by any of the array elements.
Examples:
Input: arr[] = {2, 3, 4, 5, 6}, L = 1, R = 20
Output: 6
Explanation:
The 6 numbers in the range [1, 20] that are not divisible by any of the array elements are 1, 7, 11, 13, 17 and 19.
Input: arr[] = {1, 2, 3}, L = 75, R = 1000000
Output: 0
Explanation:
Since all the numbers are divisible by 1, therefore, the answer is 0.
Naive Approach: The simple approach is to iterate through all the numbers in the given range [L, R], and for every number, check if it is divisible by any of the array elements. If it is not divisible by any of the array elements, increment the count. After checking for all the numbers, print the count.
Time Complexity: O((R – L + 1)*N)
Auxiliary Space: O(N)
Efficient Approach: The above approach can be optimized by using Sieve of Eratosthenes, marking all the multiples of a number and storing them in an efficient data structure, say Set which provides a lookup operation in almost constant time. Follow the steps below to solve the problem:
- First, for each array element, say arr[i], store all its multiples, smaller than R, in a Set using Sieve of Eratosthenes.
- The number of integers in the range [1, R] that are not divisible by any number present in the given array will be equal to (R – size of the set). Let it be A.
- Similarly, find the numbers in the range [1, L] that are not divisible by any number present in the given array. Let it be B.
- After the above steps, print the value of (A – B) as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findNonMultiples( int arr[],
int n, int k)
{
set< int > multiples;
for ( int i = 0; i < n; ++i) {
if (multiples.find(arr[i])
== multiples.end()) {
for ( int j = 1;
j <= k / arr[i]; j++) {
multiples.insert(arr[i] * j);
}
}
}
return k - multiples.size();
}
int countValues( int arr[], int N,
int L, int R)
{
return findNonMultiples(arr, N, R)
- findNonMultiples(arr, N, L - 1);
}
int main()
{
int arr[] = { 2, 3, 4, 5, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
int L = 1, R = 20;
cout << countValues(arr, N, L, R);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
public static int findNonMultiples( int [] arr, int n,
int k)
{
Set<Integer> multiples = new HashSet<Integer>();
for ( int i = 0 ; i < n; ++i)
{
if (!multiples.contains(arr[i]))
{
for ( int j = 1 ; j <= k / arr[i]; j++)
{
multiples.add(arr[i] * j);
}
}
}
return k - multiples.size();
}
public static int countValues( int [] arr, int N,
int L, int R)
{
return findNonMultiples(arr, N, R) -
findNonMultiples(arr, N, L - 1 );
}
public static void main(String[] args)
{
int [] arr = { 2 , 3 , 4 , 5 , 6 };
int N = arr.length;
int L = 1 ;
int R = 20 ;
System.out.println(countValues(arr, N, L, R));
}
}
|
Python3
def findNonMultiples(arr, n, k):
multiples = set ([])
for i in range (n):
if (arr[i] not in multiples):
for j in range ( 1 , k / / arr[i] + 1 ):
multiples.add(arr[i] * j)
return k - len (multiples)
def countValues(arr, N, L, R):
return (findNonMultiples(arr, N, R) -
findNonMultiples(arr, N, L - 1 ))
if __name__ = = "__main__" :
arr = [ 2 , 3 , 4 , 5 , 6 ]
N = len (arr)
L = 1
R = 20
print ( countValues(arr, N, L, R))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static int findNonMultiples( int [] arr, int n,
int k)
{
HashSet< int > multiples = new HashSet< int >();
for ( int i = 0; i < n; ++i)
{
if (!multiples.Contains(arr[i]))
{
for ( int j = 1; j <= k / arr[i]; j++)
{
multiples.Add(arr[i] * j);
}
}
}
return k - multiples.Count;
}
public static int countValues( int [] arr, int N,
int L, int R)
{
return findNonMultiples(arr, N, R) -
findNonMultiples(arr, N, L - 1);
}
public static void Main(String[] args)
{
int [] arr = { 2, 3, 4, 5, 6 };
int N = arr.Length;
int L = 1;
int R = 20;
Console.WriteLine(countValues(arr, N, L, R));
}
}
|
Javascript
<script>
function findNonMultiples(arr, n, k)
{
let multiples = new Set();
for (let i = 0; i < n; ++i) {
if (!multiples.has(arr[i])) {
for (let j = 1;
j <= k / arr[i]; j++) {
multiples.add(arr[i] * j);
}
}
}
return k - multiples.size;
}
function countValues(arr, N, L, R)
{
return findNonMultiples(arr, N, R)
- findNonMultiples(arr, N, L - 1);
}
let arr = [ 2, 3, 4, 5, 6 ];
let N = arr.length;
let L = 1, R = 20;
document.write(countValues(arr, N, L, R));
</script>
|
Time Complexity: O(N*log(log N))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...