Given positive integers **N** and **K**, the task is to find the highest and smallest power of K greater than equal to and less than equal to N respectively.**Examples:**

Input:N = 3, K = 2Output:2 4

Highest power of 2 less than 3 = 2

Smallest power of 2 greater than 3 = 4Input:N = 6, K = 3Output:3 9

Highest power of 3 less than 6 = 2

Smallest power of 3 greater than 6 = 9

**Approach:**

- Compute the log of N in base K (
**log**) to get the exponential power such that K raised to this exponent is the Highest power of K less than equal to N._{K}N - For the Smallest power of K less than equal to N, find the next power of K computed from the last step

Below is the implementation of the above approach:

`// C++ implementation of the approach` `#include <bits/stdc++.h>` `using` `namespace` `std;`
`// Function to return the highest power` `// of k less than or equal to n` `int` `prevPowerofK(` `int` `n, ` `int` `k)`
`{` ` ` `int` `p = (` `int` `)(` `log` `(n) / ` `log` `(k));`
` ` `return` `(` `int` `)` `pow` `(k, p);`
`}` `// Function to return the smallest power` `// of k greater than or equal to n` `int` `nextPowerOfK(` `int` `n, ` `int` `k)`
`{` ` ` `return` `prevPowerofK(n, k) * k;`
`}` `// Function to print the result` `void` `printResult(` `int` `n, ` `int` `k)`
`{` ` ` `cout << prevPowerofK(n, k)`
` ` `<< ` `" "` `<< nextPowerOfK(n, k)`
` ` `<< endl;`
`}` `// Driver code` `int` `main()`
`{` ` ` `int` `n = 25, k = 3;`
` ` `printResult(n, k);`
` ` `return` `0;`
`}` |

*chevron_right*

*filter_none*

`// Java implementation of the approach` `import` `java.io.*;`
`class` `GFG{`
`// Function to return the highest power` `// of k less than or equal to n` `static` `int` `prevPowerofK(` `int` `n, ` `int` `k)`
`{` ` ` `int` `p = (` `int` `)(Math.log(n) / Math.log(k));`
` ` `return` `(` `int` `) Math.pow(k, p);`
`}` `// Function to return the smallest power` `// of k greater than or equal to n` `static` `int` `nextPowerOfK(` `int` `n, ` `int` `k)`
`{` ` ` `return` `prevPowerofK(n, k) * k;`
`}` `// Function to print the result` `static` `void` `printResult(` `int` `n, ` `int` `k)`
`{` ` ` `System.out.println(prevPowerofK(n, k) + ` `" "` `+ `
` ` `nextPowerOfK(n, k));`
`}` `// Driver Code` `public` `static` `void` `main (String args[])`
`{` ` ` `int` `n = ` `25` `, k = ` `3` `;`
` ` `printResult(n, k);`
`}` `}` `// This code is contributed by shivanisinghss2110` |

*chevron_right*

*filter_none*

`# Python3 implementation of the approach` `import` `math`
`# Function to return the highest power` `# of k less than or equal to n` `def` `prevPowerofK(n, k):`
` ` `p ` `=` `int` `(math.log(n) ` `/` `math.log(k))`
` ` `return` `int` `(math.` `pow` `(k, p))`
`# Function to return the smallest power` `# of k greater than or equal to n` `def` `nextPowerOfK(n, k):`
` ` `return` `prevPowerofK(n, k) ` `*` `k`
`# Function to print the result` `def` `printResult(n, k):`
` ` `print` `(prevPowerofK(n, k), nextPowerOfK(n, k))`
`# Driver code` `n ` `=` `6`
`k ` `=` `3`
`printResult(n, k)` `# This code is contributed by divyamohan123` |

*chevron_right*

*filter_none*

`// C# implementation of the approach` `using` `System;`
`class` `GFG{`
`// Function to return the highest power` `// of k less than or equal to n` `static` `int` `prevPowerofK(` `int` `n, ` `int` `k)`
`{` ` ` `int` `p = (` `int` `)(Math.Log(n) / Math.Log(k));`
` ` `return` `(` `int` `) Math.Pow(k, p);`
`}` `// Function to return the smallest power` `// of k greater than or equal to n` `static` `int` `nextPowerOfK(` `int` `n, ` `int` `k)`
`{` ` ` `return` `prevPowerofK(n, k) * k;`
`}` `// Function to print the result` `static` `void` `printResult(` `int` `n, ` `int` `k)`
`{` ` ` `Console.WriteLine(prevPowerofK(n, k) + ` `" "` `+ `
` ` `nextPowerOfK(n, k));`
`}` `// Driver Code` `public` `static` `void` `Main(String []args)`
`{` ` ` `int` `n = 25, k = 3;`
` ` `printResult(n, k);`
`}` `}` `// This code is contributed by gauravrajput1` |

*chevron_right*

*filter_none*

**Output:**

9 27

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Highest power of 2 less than or equal to given number
- Highest power of 2 less than or equal to given Integer
- Smallest power of 2 greater than or equal to n
- Smallest power of 4 greater than or equal to N
- Smallest subarray of size greater than K with sum greater than a given value
- Minimize array sum by replacing greater and smaller elements of pairs by half and double of their values respectively atmost K times
- Smallest number greater than n that can be represented as a sum of distinct power of k
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Highest power of a number that divides other number
- Highest power of 2 that divides a number represented in binary
- Count of pairs in an array such that the highest power of 2 that divides their product is 1
- Highest power of 2 that divides the LCM of first N Natural numbers.
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Maximize array sum by replacing equal adjacent pairs by their sum and X respectively
- Largest number less than N with digit sum greater than the digit sum of N
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- Smallest number greater than or equal to N divisible by K
- Smallest Special Prime which is greater than or equal to a given number
- Smallest number greater than or equal to X whose sum of digits is divisible by Y
- Smallest subarray from a given Array with sum greater than or equal to K

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

**Practice Tags :**