Logarithm tricks for Competitive Programming | Java implementation to find the previous and next power of K ; Function to return the highest power of k less than or equal to n ; Function to return the smallest power of k greater than or equal to n ; Driver Code
class GFG { static int prevPowerofK ( int n , int k ) { int p = ( int ) ( Math . log ( n ) / Math . log ( k ) ) ; return ( int ) Math . pow ( k , p ) ; } static int nextPowerOfK ( int n , int k ) { return prevPowerofK ( n , k ) * k ; } public static void main ( String [ ] args ) { int N = 7 ; int K = 2 ; System . out . print ( prevPowerofK ( N , K ) + " ▁ " ) ; System . out . println ( nextPowerOfK ( N , K ) ) ; } }