Count elements in the given range which have maximum number of divisors | 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
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 ) ) ;