Skip to content
Related Articles

Related Articles

Count numbers from a given range that are not divisible by any of the array elements
  • Last Updated : 12 Jan, 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:
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++

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 find the non-multiples
// till k
int findNonMultiples(int arr[],
                     int n, int k)
{
    // Stores all unique multiples
    set<int> multiples;
 
    // Iterate the array
    for (int i = 0; i < n; ++i) {
 
        // For finding duplicates
        // only once
        if (multiples.find(arr[i])
            == multiples.end()) {
 
            // Inserting all multiples
            // into the set
            for (int j = 1;
                 j <= k / arr[i]; j++) {
                multiples.insert(arr[i] * j);
            }
        }
    }
 
    // Returning only the count of
    // numbers that are not divisible
    // by any of the array elements
    return k - multiples.size();
}
 
// Function to count the total values
// in the range [L, R]
int countValues(int arr[], int N,
                int L, int R)
{
    // Count all values in the range
    // using exclusion principle
    return findNonMultiples(arr, N, R)
           - findNonMultiples(arr, N, L - 1);
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 3, 4, 5, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int L = 1, R = 20;
 
    // Function Call
    cout << countValues(arr, N, L, R);
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to find the non-multiples
// till k
public static int findNonMultiples(int[] arr, int n,
                                   int k)
{
     
    // Stores all unique multiples
    Set<Integer> multiples = new HashSet<Integer>();
     
    // Iterate the array
    for(int i = 0; i < n; ++i)
    {
         
        // For finding duplicates
        // only once
        if (!multiples.contains(arr[i]))
        {
             
            // Inserting all multiples
            // into the set
            for(int j = 1; j <= k / arr[i]; j++)
            {
                multiples.add(arr[i] * j);
            }
        }
    }
     
    // Returning only the count of
    // numbers that are not divisible
    // by any of the array elements
    return k - multiples.size();
}
 
// Function to count the total values
// in the range [L, R]
public static int countValues(int[] arr, int N,
                              int L, int R)
{
     
    // Count all values in the range
    // using exclusion principle
    return findNonMultiples(arr, N, R) -
           findNonMultiples(arr, N, L - 1);
}
 
// Driver code
public static void main(String[] args)
{
    int[] arr = { 2, 3, 4, 5, 6 };
    int N = arr.length;
    int L = 1;
    int R = 20;
 
    // Function Call
    System.out.println(countValues(arr, N, L, R));
}
}
 
// This code is contributed by rohitsingh07052

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
 
# Function to find the non-multiples
# till k
def findNonMultiples(arr, n, k):
 
    # Stores all unique multiples
    multiples = set([])
 
    # Iterate the array
    for i in range(n):
 
        # For finding duplicates
        # only once
        if (arr[i] not in multiples):
 
            # Inserting all multiples
            # into the set
            for j in range(1, k // arr[i] + 1):
                multiples.add(arr[i] * j)
 
    # Returning only the count of
    # numbers that are not divisible
    # by any of the array elements
    return k - len(multiples)
 
# Function to count the total values
# in the range [L, R]
def countValues(arr, N, L, R):
 
    # Count all values in the range
    # using exclusion principle
    return (findNonMultiples(arr, N, R) -
            findNonMultiples(arr, N, L - 1))
 
# Driver Code
if __name__ == "__main__":
   
    arr = [ 2, 3, 4, 5, 6 ]
    N = len(arr)
    L = 1
    R = 20
     
    # Function Call
    print( countValues(arr, N, L, R))
 
# This code is contributed by chitranayal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
 
// Function to find the non-multiples
// till k
public static int findNonMultiples(int[] arr, int n,
                                   int k)
{
     
    // Stores all unique multiples
    HashSet<int> multiples = new HashSet<int>();
     
    // Iterate the array
    for(int i = 0; i < n; ++i)
    {
         
        // For finding duplicates
        // only once
        if (!multiples.Contains(arr[i]))
        {
             
            // Inserting all multiples
            // into the set
            for(int j = 1; j <= k / arr[i]; j++)
            {
                multiples.Add(arr[i] * j);
            }
        }
    }
     
    // Returning only the count of
    // numbers that are not divisible
    // by any of the array elements
    return k - multiples.Count;
}
 
// Function to count the total values
// in the range [L, R]
public static int countValues(int[] arr, int N,
                              int L, int R)
{
     
    // Count all values in the range
    // using exclusion principle
    return findNonMultiples(arr, N, R) -
           findNonMultiples(arr, N, L - 1);
}
 
// Driver code
public static void Main(String[] args)
{
    int[] arr = { 2, 3, 4, 5, 6 };
    int N = arr.Length;
    int L = 1;
    int R = 20;
 
    // Function Call
    Console.WriteLine(countValues(arr, N, L, R));
}
}
 
// This code is contributed by shikhasingrajput

chevron_right


Output: 

6

 

Time Complexity: O(N*log(log N))
Auxiliary Space: O(N)

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 Articles
Page :