Count numbers in a range that are divisible by all array elements
Last Updated :
20 Dec, 2022
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 the elements 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 by all the 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), as we will be using nested loops, outer loop for iterating from L to R and inner loop for traversing the elements of the array.
Auxiliary Space: O(1), as we will not be using any extra space.
An efficient approach is to find the LCM of N numbers and then count the numbers that are divisible by LCM in the range [L, R]. The numbers divisible by LCM till R are R/LCM. So using the exclusion principle, the count will be (R / LCM) – ((L – 1) / LCM).
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int findLCM( int arr[], int n)
{
int lcm = arr[0];
for ( int i = 1; i < n; i++) {
lcm = (lcm * arr[i]) / __gcd(arr[i], lcm);
}
return lcm;
}
int countNumbers( int arr[], int n, int l, int r)
{
int lcm = findLCM(arr, n);
int count = (r / lcm) - ((l - 1) / lcm);
return count;
}
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;
}
|
C
#include <stdio.h>
int gcd( int x, int y)
{
while (y != 0) {
int temp = y;
y = x % y;
x = temp;
}
return x;
}
int findLCM( int arr[], int n)
{
int lcm = arr[0];
for ( int i = 1; i < n; i++) {
lcm = (lcm * arr[i]) / gcd(arr[i], lcm);
}
return lcm;
}
int countNumbers( int arr[], int n, int l, int r)
{
int lcm = findLCM(arr, n);
int count = (r / lcm) - ((l - 1) / lcm);
return count;
}
int main()
{
int arr[] = { 1, 4, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
int l = 1, r = 10;
printf ( "%d" , countNumbers(arr, n, l, r));
return 0;
}
|
Java
class GFG {
static int __gcd( int a, int b)
{
if (a == 0 || b == 0 )
return 0 ;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
static int findLCM( int arr[], int n)
{
int lcm = arr[ 0 ];
for ( int i = 1 ; i < n; i++) {
lcm = (lcm * arr[i]) / __gcd(arr[i], lcm);
}
return lcm;
}
static int countNumbers( int arr[], int n, int l, int r)
{
int lcm = findLCM(arr, n);
int count = (r / lcm) - ((l - 1 ) / lcm);
return count;
}
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));
}
}
|
Python3
import math
def findLCM(arr, n):
lcm = arr[ 0 ]
for i in range ( 1 , n - 1 ):
lcm = (lcm * arr[i]) / math.gcd(arr[i], lcm)
return lcm
def countNumbers(arr, n, l, r):
lcm = int (findLCM(arr, n))
count = (r / lcm) - ((l - 1 ) / lcm)
print ( int (count))
arr = [ 1 , 4 , 2 ]
n = len (arr)
l = 1
r = 10
countNumbers(arr, n, l, r)
|
C#
using System;
class GFG
{
static int __gcd( int a, int b)
{
if (a == 0 || b == 0)
return 0;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
static int findLCM( int []arr, int n)
{
int lcm = arr[0];
for ( int i = 1; i < n; i++)
{
lcm = (lcm * arr[i]) / __gcd(arr[i], lcm);
}
return lcm;
}
static int countNumbers( int []arr, int n,
int l, int r)
{
int lcm = findLCM(arr, n);
int count = (r / lcm) - ((l - 1) / lcm);
return count;
}
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));
}
}
|
PHP
<?php
function __gcd( $a , $b )
{
if ( $a == 0 || $b == 0)
return 0;
if ( $a == $b )
return $a ;
if ( $a > $b )
return __gcd( $a - $b , $b );
return __gcd( $a , $b - $a );
}
function findLCM( $arr , $n )
{
$lcm = $arr [0];
for ( $i = 1; $i < $n ; $i ++)
{
$lcm = ( $lcm * $arr [ $i ]) /
__gcd( $arr [ $i ], $lcm );
}
return $lcm ;
}
function countNumbers( $arr , $n , $l , $r )
{
$lcm = findLCM( $arr , $n );
$count = (int)( $r / $lcm ) -
(int)(( $l - 1) / $lcm );
return $count ;
}
$arr = array (1, 4, 2);
$n = sizeof( $arr );
$l = 1; $r = 10;
echo countNumbers( $arr , $n , $l , $r );
?>
|
Javascript
<script>
function __gcd(a , b) {
if (a == 0 || b == 0)
return 0;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
function findLCM(arr , n) {
var lcm = arr[0];
for (i = 1; i < n; i++) {
lcm = parseInt((lcm * arr[i]) / __gcd(arr[i], lcm));
}
return lcm;
}
function countNumbers(arr , n , l , r) {
var lcm = findLCM(arr, n);
var count = parseInt((r / lcm) - ((l - 1) / lcm));
return count;
}
var arr = [ 1, 4, 2 ];
var n = arr.length;
var l = 1, r = 10;
document.write(countNumbers(arr, n, l, r));
</script>
|
Output:
2
Complexity Analysis:
- Time Complexity: O(N*log(max element of array)), as we are using a loop for traversing N times and gcd function to find the lcm in each traversal.
- Auxiliary Space: O(1), as we are not using any extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...