Largest value of x such that axx is N | Java implementation of the above approach ; Function to find log_b ( a ) ; Set two pointer for binary search ; Calculating number of digits of a * mid ^ mid in base b ; If number of digits > n we can simply ignore it and decrease our pointer ; If number of digits <= n , we can go higher to reach value exactly equal to n ; Return the largest value of x ; Driver Code
import java . util . * ; class GFG { static int log ( int a , int b ) { return ( int ) ( Math . log10 ( a ) / Math . log10 ( b ) ) ; } static int get ( int a , int b , int n ) { int lo = 0 , hi = ( int ) 1e6 ; int ans = 0 ; while ( lo <= hi ) { int mid = ( lo + hi ) / 2 ; int dig = ( int ) Math . ceil ( ( mid * log ( mid , b ) + log ( a , b ) ) ) ; if ( dig > n ) { hi = mid - 1 ; } else { ans = mid ; lo = mid + 1 ; } } return ans ; } public static void main ( String [ ] args ) { int a = 2 , b = 2 , n = 6 ; System . out . print ( get ( a , b , n ) + "NEW_LINE"); } }