Count elements in the given range which have maximum number of divisors | C ++ implementation of above approach ; Function to count the elements with maximum number of divisors ; to store number of divisors ; initialise with zero ; to store the maximum number of divisors ; to store required answer ; Find the first divisible number ; Count number of divisors ; Find number of elements with maximum number of divisors ; Driver code
#include <bits/stdc++.h> NEW_LINE 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 ; }