Count numbers in a range that are divisible by all array elements

Given N numbers and two numbers L and R., the task is to print the count of numbers in the range [L, R] which are divisible by all numbers of the array.

Examples:

Input: a[] = {1, 4, 2], L = 1, R = 10
Output : 2
In range [1, 10], the numbers 4 and 8 are divisible all array elements.

Input : a[] = {1, 3, 2], L = 7, R = 11
Output : 0



A naive approach is to iterate from L to R and count the numbers which are divisible by all of the array elements.

Time Complexity: O((R-L) * N)

An efficient approach is to find the LCM of N numbers and then count the numbers that are divisible by LCM in range L and R. The numbers divisible by LCM till R are R/LCM. So using exclusion principle, the count will be (R/LCM – L-1/LCM).

Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to count numbers in a range 
// that are divisible by all array elements
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the lcm of array
int findLCM(int arr[], int n)
{
    int lcm = arr[0];
  
    // Iterate in the array
    for (int i = 1; i < n; i++) {
  
        // Find lcm
        lcm = (lcm * arr[i]) / __gcd(arr[i], lcm);
    }
  
    return lcm;
}
  
// Function to return the count of numbers
int countNumbers(int arr[], int n, int l, int r)
{
  
    // Function call to find the
    // LCM of N numbers
    int lcm = findLCM(arr, n);
  
    // Return the count of numbers
    int count = (r / lcm) - ((l - 1) / lcm);
}
  
// Driver Code
int main()
{
    int arr[] = { 1, 4, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int l = 1, r = 10;
  
    cout << countNumbers(arr, n, l, r);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to count numbers in a range 
// that are divisible by all array elements
class GFG
{
      
// Function to calculate gcd
static int __gcd(int a, int b)
{
        
    // Everything divides 0 
    if (a == 0 || b == 0)
        return 0;
    
    // base case
    if (a == b)
        return a;
    
    // a is greater
    if (a > b)
        return __gcd(a - b, b);
            
    return __gcd(a, b - a);
}
      
// Function to find the lcm of array
static int findLCM(int arr[], int n)
{
    int lcm = arr[0];
  
    // Iterate in the array
    for (int i = 1; i < n; i++) 
    {
  
        // Find lcm
        lcm = (lcm * arr[i]) / __gcd(arr[i], lcm);
    }
  
    return lcm;
}
  
// Function to return the count of numbers
static int countNumbers(int arr[], int n, 
                        int l, int r)
{
  
    // Function call to find the
    // LCM of N numbers
    int lcm = findLCM(arr, n);
  
    // Return the count of numbers
    int count = (r / lcm) - ((l - 1) / lcm);
  
    return count;
}
  
// Driver Code
public static void main(String args[])
{
    int arr[] = { 1, 4, 2 };
    int n = arr.length;
    int l = 1, r = 10;
  
    System.out.println(countNumbers(arr, n, l, r));
}
}
  
// This code is contributed by Mukul Singh

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to count numbers in 
# a range that are divisible by all
# array elements
import math
  
# Function to find the lcm of array
def findLCM(arr, n):
  
    lcm = arr[0];
  
    # Iterate in the array
    for i in range(1, n - 1): 
  
        # Find lcm
        lcm = (lcm * arr[i]) / math.gcd(arr[i], lcm);
  
    return lcm;
  
# Function to return the count of numbers
def countNumbers(arr, n, l, r):
  
    # Function call to find the
    # LCM of N numbers
    lcm = int(findLCM(arr, n));
      
    # Return the count of numbers
    count = (r / lcm) - ((l - 1) / lcm);
    print(int(count));
  
# Driver Code
arr = [1, 4, 2];
n = len(arr);
l = 1;
r = 10;
  
countNumbers(arr, n, l, r);
  
# This code is contributed 
# by Shivi_Aggarwal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to count numbers in a range 
// that are divisible by all array elements 
using System;
  
class GFG 
      
// Function to calculate gcd 
static int __gcd(int a, int b) 
          
    // Everything divides 0 
    if (a == 0 || b == 0) 
        return 0; 
      
    // base case 
    if (a == b) 
        return a; 
      
    // a is greater 
    if (a > b) 
        return __gcd(a - b, b); 
              
    return __gcd(a, b - a); 
      
// Function to find the lcm of array 
static int findLCM(int []arr, int n) 
    int lcm = arr[0]; 
  
    // Iterate in the array 
    for (int i = 1; i < n; i++) 
    
  
        // Find lcm 
        lcm = (lcm * arr[i]) / __gcd(arr[i], lcm); 
    
  
    return lcm; 
  
// Function to return the count of numbers 
static int countNumbers(int []arr, int n, 
                        int l, int r) 
  
    // Function call to find the 
    // LCM of N numbers 
    int lcm = findLCM(arr, n); 
  
    // Return the count of numbers 
    int count = (r / lcm) - ((l - 1) / lcm); 
  
    return count; 
  
// Driver Code 
public static void Main() 
    int []arr = { 1, 4, 2 }; 
    int n = arr.Length; 
    int l = 1, r = 10; 
  
    Console.WriteLine(countNumbers(arr, n, l, r)); 
  
// This code is contributed by Ryuga 

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to count numbers in a range 
// that are divisible by all array elements
  
// Function to calculate gcd
function __gcd($a, $b)
{
      
    // Everything divides 0 
    if ($a == 0 || $b == 0)
        return 0;
  
    // base case
    if ($a == $b)
        return $a;
  
    // a is greater
    if ($a > $b)
        return __gcd($a - $b, $b);
          
    return __gcd($a, $b - $a);
}
      
// Function to find the lcm of array
function findLCM($arr, $n)
{
    $lcm = $arr[0];
  
    // Iterate in the array
    for ($i = 1; $i < $n; $i++) 
    {
  
        // Find lcm
        $lcm = ($lcm * $arr[$i]) / 
                 __gcd($arr[$i], $lcm);
    }
  
    return $lcm;
}
  
// Function to return the count of numbers
function countNumbers($arr, $n, $l, $r)
{
  
    // Function call to find the
    // LCM of N numbers
    $lcm = findLCM($arr, $n);
  
    // Return the count of numbers
    $count = (int)($r / $lcm) -
             (int)(($l - 1) / $lcm);
  
    return $count;
}
  
// Driver Code
$arr = array(1, 4, 2);
$n = sizeof($arr);
$l = 1; $r = 10;
echo countNumbers($arr, $n, $l, $r);
  
// This code is contributed
// by Akanksha Rai
?>

chevron_right


Output:

2


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

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.