Count elements in the given range which have maximum number of divisors
Last Updated :
05 Mar, 2023
Given two numbers X and Y. The task is to find the number of elements in the range [X,Y] both inclusive, that have the maximum number of divisors.
Examples:
Input: X = 2, Y = 9
Output: 2
6, 8 are numbers with the maximum number of divisors.
Input: X = 1, Y = 10
Output: 3
6, 8, 10 are numbers with the maximum number of divisors.
Method 1:
- Traverse all the elements from X to Y one by one.
- Find the number of divisors of each element.
- Store the number of divisors in an array and update the maximum number of Divisors(maxDivisors).
- Traverse the array that contains divisors and counts the number of elements equal to maxDivisors.
- Return the count.
Below is the implementation of above method:
C++
#include <bits/stdc++.h>
using namespace std;
int countDivisors( int n)
{
int count = 0;
for ( int i = 1; i <= sqrt (n); i++) {
if (n % i == 0) {
if (n / i == i)
count++;
else
count += 2;
}
}
return count;
}
int MaximumDivisors( int X, int Y)
{
int maxDivisors = INT_MIN, result = 0;
int arr[Y - X + 1];
for ( int i = X; i <= Y; i++) {
int Div = countDivisors(i);
arr[i - X] = Div;
maxDivisors = max(Div, maxDivisors);
}
for ( int i = 0; i < (Y - X + 1); i++)
if (arr[i] == maxDivisors)
result++;
return result;
}
int main()
{
int X = 1, Y = 10;
cout << MaximumDivisors(X, Y) << endl;
return 0;
}
|
C
#include <stdio.h>
#include <math.h>
#include <limits.h>
int max( int a, int b)
{
int max = a;
if (max < b)
max = b;
return max;
}
int countDivisors( int n)
{
int count = 0;
for ( int i = 1; i <= sqrt (n); i++) {
if (n % i == 0) {
if (n / i == i)
count++;
else
count += 2;
}
}
return count;
}
int MaximumDivisors( int X, int Y)
{
int maxDivisors = INT_MIN, result = 0;
int arr[Y - X + 1];
for ( int i = X; i <= Y; i++) {
int Div = countDivisors(i);
arr[i - X] = Div;
maxDivisors = max(Div, maxDivisors);
}
for ( int i = 0; i < (Y - X + 1); i++)
if (arr[i] == maxDivisors)
result++;
return result;
}
int main()
{
int X = 1, Y = 10;
printf ( "%d\n" ,MaximumDivisors(X, Y));
return 0;
}
|
Java
class GFG
{
static int countDivisors( int n)
{
int count = 0 ;
for ( int i = 1 ; i <= Math.sqrt(n); i++)
{
if (n % i == 0 )
{
if (n / i == i)
count++;
else
count += 2 ;
}
}
return count;
}
static int MaximumDivisors( int X, int Y)
{
int maxDivisors = 0 , result = 0 ;
int [] arr = new int [Y - X + 1 ];
for ( int i = X; i <= Y; i++)
{
int Div = countDivisors(i);
arr[i - X] = Div;
maxDivisors = Math.max(Div, maxDivisors);
}
for ( int i = 0 ; i < (Y - X + 1 ); i++)
if (arr[i] == maxDivisors)
result++;
return result;
}
public static void main(String[] args)
{
int X = 1 , Y = 10 ;
System.out.println(MaximumDivisors(X, Y));
}
}
|
Python3
from math import *
def countDivisors(n) :
count = 0
for i in range ( 1 , int (sqrt(n) + 1 )) :
if n % i = = 0 :
if n / i = = i :
count + = 1
else :
count + = 2
return count
def MaximumDivisors(X,Y) :
result = 0
maxDivisors = 0
arr = []
for i in range (Y - X + 1 ) :
arr.append( 0 )
for i in range (X,Y + 1 ) :
Div = countDivisors(i)
arr[i - X] = Div
maxDivisors = max (Div,maxDivisors)
for i in range (Y - X + 1 ) :
if arr[i] = = maxDivisors :
result + = 1
return result
if __name__ = = "__main__" :
X, Y = 1 , 10
print (MaximumDivisors(X,Y))
|
C#
using System;
class GFG
{
static int countDivisors( int n)
{
int count = 0;
for ( int i = 1; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
if (n / i == i)
count++;
else
count += 2;
}
}
return count;
}
static int MaximumDivisors( int X, int Y)
{
int maxDivisors = 0, result = 0;
int [] arr = new int [Y - X + 1];
for ( int i = X; i <= Y; i++)
{
int Div = countDivisors(i);
arr[i - X] = Div;
maxDivisors = Math.Max(Div, maxDivisors);
}
for ( int i = 0; i < (Y - X + 1); i++)
if (arr[i] == maxDivisors)
result++;
return result;
}
public static void Main()
{
int X = 1, Y = 10;
Console.Write(MaximumDivisors(X, Y));
}
}
|
PHP
<?php
function countDivisors( $n )
{
$count = 0;
for ( $i = 1; $i <= sqrt( $n ); $i ++)
{
if ( $n % $i == 0)
{
if ( $n / $i == $i )
$count ++;
else
$count += 2;
}
}
return $count ;
}
function MaximumDivisors( $X , $Y )
{
$maxDivisors = PHP_INT_MIN;
$result = 0;
$arr = array_fill (0, ( $Y - $X + 1), NULL);
for ( $i = $X ; $i <= $Y ; $i ++)
{
$Div = countDivisors( $i );
$arr [ $i - $X ] = $Div ;
$maxDivisors = max( $Div , $maxDivisors );
}
for ( $i = 0; $i < ( $Y - $X + 1); $i ++)
if ( $arr [ $i ] == $maxDivisors )
$result ++;
return $result ;
}
$X = 1;
$Y = 10;
echo MaximumDivisors( $X , $Y ). " " ;
?>
|
Javascript
<script>
function countDivisors(n)
{
let count = 0;
for (let i = 1; i <= Math.sqrt(n); i++)
{
if (n % i == 0)
{
if (Math.floor(n / i) == i)
count++;
else
count += 2;
}
}
return count;
}
function MaximumDivisors(X, Y)
{
let maxDivisors = 0, result = 0;
let arr = new Array(Y - X + 1);
for (let i = X; i <= Y; i++)
{
let Div = countDivisors(i);
arr[i - X] = Div;
maxDivisors = Math.max(Div, maxDivisors);
}
for (let i = 0; i < (Y - X + 1); i++)
if (arr[i] == maxDivisors)
result++;
return result;
}
let X = 1, Y = 10;
document.write(MaximumDivisors(X, Y));
</script>
|
Time Complexity: O(sqrt(n))
Auxiliary Space: O(n)
Method 2:
- Create an array of size Y-X+1 to store number of divisors of X in arr[0], X+1 in arr[1]… up to Y.
- To get the number of divisors of all numbers from X to Y run two loops(nested).
- Run an outer loop from 1 to sqrt(Y).
- Run an inner loop from first_divisible to Y.
- First_divisible is the number which is the first number to be divisible by I(outer loop) and greater than or equals to X.
Here, first_divisible is calculated by using Find the number closest to n and divisible by m method. Then find divisors of the number.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MaximumDivisors( int X, int Y)
{
int arr[Y - X + 1];
memset (arr, 0, sizeof (arr));
int mx = INT_MIN;
int cnt = 0;
for ( int i = 1; i * i <= Y; i++) {
int sq = i * i;
int first_divisible;
if ((X / i) * i >= X)
first_divisible = (X / i) * i;
else
first_divisible = (X / i + 1) * i;
for ( int j = first_divisible; j <= Y; j += i) {
if (j < sq)
continue ;
else if (j == sq)
arr[j - X]++;
else
arr[j - X] += 2;
}
}
for ( int i = X; i <= Y; i++) {
if (arr[i - X] > mx) {
cnt = 1;
mx = arr[i - X];
}
else if (arr[i - X] == mx)
cnt++;
}
return cnt;
}
int main()
{
int X = 1, Y = 10;
cout << MaximumDivisors(X, Y) << endl;
return 0;
}
|
C
#include <stdio.h>
#include <limits.h>
#include <string.h>
int MaximumDivisors( int X, int Y)
{
int arr[Y - X + 1];
memset (arr, 0, sizeof (arr));
int mx = INT_MIN;
int cnt = 0;
for ( int i = 1; i * i <= Y; i++) {
int sq = i * i;
int first_divisible;
if ((X / i) * i >= X)
first_divisible = (X / i) * i;
else
first_divisible = (X / i + 1) * i;
for ( int j = first_divisible; j <= Y; j += i) {
if (j < sq)
continue ;
else if (j == sq)
arr[j - X]++;
else
arr[j - X] += 2;
}
}
for ( int i = X; i <= Y; i++) {
if (arr[i - X] > mx) {
cnt = 1;
mx = arr[i - X];
}
else if (arr[i - X] == mx)
cnt++;
}
return cnt;
}
int main()
{
int X = 1, Y = 10;
printf ( "%d\n" ,MaximumDivisors(X, Y));
return 0;
}
|
Java
class GFG
{
static int MaximumDivisors( int X, int Y)
{
int [] arr = new int [Y - X + 1 ];
for ( int i = 0 ; i < arr.length; i++)
arr[i] = 0 ;
int mx = 0 ;
int cnt = 0 ;
for ( int i = 1 ; i * i <= Y; i++)
{
int sq = i * i;
int first_divisible;
if ((X / i) * i >= X)
first_divisible = (X / i) * i;
else
first_divisible = (X / i + 1 ) * i;
for ( int j = first_divisible;
j <= Y; j += i)
{
if (j < sq)
continue ;
else if (j == sq)
arr[j - X]++;
else
arr[j - X] += 2 ;
}
}
for ( int i = X; i <= Y; i++)
{
if (arr[i - X] > mx)
{
cnt = 1 ;
mx = arr[i - X];
}
else if (arr[i - X] == mx)
cnt++;
}
return cnt;
}
public static void main(String[] args)
{
int X = 1 , Y = 10 ;
System.out.println(MaximumDivisors(X, Y));
}
}
|
Python3
def MaximumDivisors(X, Y):
arr = [ 0 ] * (Y - X + 1 )
mx = 0
cnt = 0
i = 1
while i * i < = Y :
sq = i * i
if ((X / / i) * i > = X) :
first_divisible = (X / / i) * i
else :
first_divisible = (X / / i + 1 ) * i
for j in range (first_divisible, Y + 1 , i):
if j < sq :
continue
elif j = = sq :
arr[j - X] + = 1
else :
arr[j - X] + = 2
i + = 1
for i in range (X, Y + 1 ):
if arr[i - X] > mx :
cnt = 1
mx = arr[i - X]
elif arr[i - X] = = mx :
cnt + = 1
return cnt
if __name__ = = "__main__" :
X = 1
Y = 10
print (MaximumDivisors(X, Y))
|
C#
using System;
class GFG
{
static int MaximumDivisors( int X, int Y)
{
int [] arr = new int [Y - X + 1];
for ( int i = 0; i < arr.Length; i++)
arr[i] = 0;
int mx = 0;
int cnt = 0;
for ( int i = 1; i * i <= Y; i++)
{
int sq = i * i;
int first_divisible;
if ((X / i) * i >= X)
first_divisible = (X / i) * i;
else
first_divisible = (X / i + 1) * i;
for ( int j = first_divisible;
j <= Y; j += i)
{
if (j < sq)
continue ;
else if (j == sq)
arr[j - X]++;
else
arr[j - X] += 2;
}
}
for ( int i = X; i <= Y; i++)
{
if (arr[i - X] > mx)
{
cnt = 1;
mx = arr[i - X];
}
else if (arr[i - X] == mx)
cnt++;
}
return cnt;
}
public static void Main()
{
int X = 1, Y = 10;
Console.Write(MaximumDivisors(X, Y));
}
}
|
PHP
<?php
function MaximumDivisors( $X , $Y )
{
$arr = array_fill (0,( $Y - $X + 1), NULL);
$mx = PHP_INT_MIN;
$cnt = 0;
for ( $i = 1; $i * $i <= $Y ; $i ++)
{
$sq = $i * $i ;
if (( $X / $i ) * $i >= $X )
$first_divisible = ( $X / $i ) * $i ;
else
$first_divisible = ( $X / $i + 1) * $i ;
for ( $j = $first_divisible ;
$j < $Y ; $j += $i )
{
if ( $j < $sq )
continue ;
else if ( $j == $sq )
$arr [ $j - $X ]++;
else
$arr [ $j - $X ] += 2;
}
}
for ( $i = $X ; $i <= $Y ; $i ++)
{
if ( $arr [ $i - $X ] > $mx )
{
$cnt = 1;
$mx = $arr [ $i - $X ];
}
else if ( $arr [ $i - $X ] == $mx )
$cnt ++;
}
return $cnt ;
}
$X = 1;
$Y = 10;
echo MaximumDivisors( $X , $Y ). "\n" ;
?>
|
Javascript
<script>
function MaximumDivisors(X, Y)
{
let arr = new Array(Y - X + 1);
for (let i = 0; i < arr.length; i++)
arr[i] = 0;
let mx = 0;
let cnt = 0;
for (let i = 1; i * i <= Y; i++)
{
let sq = i * i;
let first_divisible;
if (Math.floor(X / i) * i >= X)
first_divisible = Math.floor(X / i) * i;
else
first_divisible = (Math.floor(X / i) +
1) * i;
for (let j = first_divisible;
j <= Y; j += i)
{
if (j < sq)
continue ;
else if (j == sq)
arr[j - X]++;
else
arr[j - X] += 2;
}
}
for (let i = X; i <= Y; i++)
{
if (arr[i - X] > mx)
{
cnt = 1;
mx = arr[i - X];
}
else if (arr[i - X] == mx)
cnt++;
}
return cnt;
}
let X = 1, Y = 10;
document.write(MaximumDivisors(X, Y));
</script>
|
Time Complexity : O((Y – X + 1) * sqrt(Y))
Space Complexity : O(Y – X + 1)
Share your thoughts in the comments
Please Login to comment...