Querying maximum number of divisors that a number in a given range has | Java implementation of the above idea to process queries of finding a number with maximum divisors . ; Finds smallest prime factor of all numbersin range [ 1 , maxn ) and stores them in smallest_prime [ ] , smallest_prime [ i ] should contain the smallest prime that divides i ; Initialize the smallest_prime factors of all to infinity ; to be built like eratosthenes sieve ; prime number will have its smallest_prime equal to itself ; if ' i ' is the first prime number reaching ' j ' ; number of divisors of n = ( p1 ^ k1 ) * ( p2 ^ k2 ) . . . ( pn ^ kn ) are equal to ( k1 + 1 ) * ( k2 + 1 ) . . . ( kn + 1 ) this function finds the number of divisors of all numbersin range [ 1 , maxn ) and stores it in divisors [ ] divisors [ i ] stores the number of divisors i has ; we can obtain the prime factorization of the number n , n = ( p1 ^ k1 ) * ( p2 ^ k2 ) . . . ( pn ^ kn ) using the smallest_prime [ ] array , we keep dividing n by its smallest_prime until it becomes 1 , whilst we check if we have need to set k zero ; use p ^ k , initialize k to 0 ; builds segment tree for divisors [ ] array ; leaf node ; build left and right subtree ; combine the information from left and right subtree at current node ; returns the maximum number of divisors in [ l , r ] ; If current node 's range is disjoint  with query range ; If the current node stores information for the range that is completely inside the query range ; Returns maximum number of divisors from left or right subtree ; Driver Code ; First find smallest prime divisors for all the numbers ; Then build the divisors [ ] array to store the number of divisors ; Build segment tree for the divisors [ ] array
import java . util . * ; class GFG { static int maxn = 10005 ; static int INF = 999999 ; static int [ ] smallest_prime = new int [ maxn ] ; static int [ ] divisors = new int [ maxn ] ; static int [ ] segmentTree = new int [ 4 * maxn ] ; static void findSmallestPrimeFactors ( ) { for ( int i = 0 ; i < maxn ; i ++ ) smallest_prime [ i ] = INF ; for ( int i = 2 ; i < maxn ; i ++ ) { if ( smallest_prime [ i ] == INF ) { smallest_prime [ i ] = i ; for ( int j = i * i ; j < maxn ; j += i ) if ( smallest_prime [ j ] > i ) smallest_prime [ j ] = i ; } } } static void buildDivisorsArray ( ) { for ( int i = 1 ; i < maxn ; i ++ ) { divisors [ i ] = 1 ; int n = i , p = smallest_prime [ i ] , k = 0 ; while ( n > 1 ) { n = n / p ; k ++ ; if ( smallest_prime [ n ] != p ) { divisors [ i ] = divisors [ i ] * ( k + 1 ) ; k = 0 ; } p = smallest_prime [ n ] ; } } } static void buildSegtmentTree ( int node , int a , int b ) { if ( a == b ) { segmentTree [ node ] = divisors [ a ] ; return ; } buildSegtmentTree ( 2 * node , a , ( a + b ) / 2 ) ; buildSegtmentTree ( 2 * node + 1 , ( ( a + b ) / 2 ) + 1 , b ) ; segmentTree [ node ] = Math . max ( segmentTree [ 2 * node ] , segmentTree [ 2 * node + 1 ] ) ; } static int query ( int node , int a , int b , int l , int r ) { if ( l > b a > r ) return - 1 ; if ( a >= l && b <= r ) return segmentTree [ node ] ; return Math . max ( query ( 2 * node , a , ( a + b ) / 2 , l , r ) , query ( 2 * node + 1 , ( ( a + b ) / 2 ) + 1 , b , l , r ) ) ; } public static void main ( String [ ] args ) { findSmallestPrimeFactors ( ) ; buildDivisorsArray ( ) ; buildSegtmentTree ( 1 , 1 , maxn - 1 ) ; System . out . println ( " Maximum ▁ divisors ▁ that ▁ a ▁ number ▁ " + " has ▁ in ▁ [ 1 , ▁ 100 ] ▁ are ▁ " + query ( 1 , 1 , maxn - 1 , 1 , 100 ) ) ; System . out . println ( " Maximum ▁ divisors ▁ that ▁ a ▁ number ▁ " + " has ▁ in ▁ [ 10 , ▁ 48 ] ▁ are ▁ " + query ( 1 , 1 , maxn - 1 , 10 , 48 ) ) ; System . out . println ( " Maximum ▁ divisors ▁ that ▁ a ▁ number ▁ " + " has ▁ in ▁ [ 1 , ▁ 10 ] ▁ are ▁ " + query ( 1 , 1 , maxn - 1 , 1 , 10 ) ) ; } }