Highest and Smallest power of K less than and greater than equal to N respectively

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 = 2Â
Output: 2 4Â
Highest power of 2 less than 3 = 2Â
Smallest power of 2 greater than 3 = 4Input: N = 6, K = 3Â
Output: 3 9Â
Highest power of 3 less than 6 = 3Â
Smallest power of 3 greater than 6 = 9Â Â
Approach:Â Â
- Compute the log of N in base K (logK N) to get the exponential power such that K raised to this exponent is the Highest power of K less than equal to 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++
// 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 nint 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 nint nextPowerOfK(int n, int k){Â Â Â Â return prevPowerofK(n, k) * k;}Â
// Function to print the resultvoid printResult(int n, int k){Â Â Â Â cout << prevPowerofK(n, k) << " " << nextPowerOfK(n, k) << endl;}Â
// Driver codeint main(){Â Â Â Â int n = 25, k = 3;Â Â Â Â printResult(n, k);Â Â Â Â return 0;}Â
// This code is contributed by Sania Kumari Gupta |
C
// C implementation of the approach#include <math.h>#include <stdio.h>Â
// Function to return the highest power of k less than or// equal to nint 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 nint nextPowerOfK(int n, int k){Â Â Â Â return prevPowerofK(n, k) * k;}Â
// Function to print the resultvoid printResult(int n, int k){Â Â Â Â printf("%d %d\n", prevPowerofK(n, k), nextPowerOfK(n, k));}Â
// Driver codeint main(){Â Â Â Â int n = 25, k = 3;Â Â Â Â printResult(n, k);Â Â Â Â return 0;}Â
// This code is contributed by Sania Kumari Gupta |
Java
// Java implementation of the approachimport java.io.*;Â
class GFG{Â
// Function to return the highest power// of k less than or equal to nstatic 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 nstatic int nextPowerOfK(int n, int k){Â Â Â Â return prevPowerofK(n, k) * k;}Â
// Function to print the resultstatic void printResult(int n, int k){Â Â Â Â System.out.println(prevPowerofK(n, k) + " " + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â nextPowerOfK(n, k));}Â
// Driver Codepublic static void main (String args[]){Â Â Â Â int n = 25, k = 3;Â Â Â Â printResult(n, k);}}Â
// This code is contributed by shivanisinghss2110 |
Python3
# Python3 implementation of the approachimport mathÂ
# Function to return the highest power# of k less than or equal to ndef 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 ndef nextPowerOfK(n, k):Â
    return prevPowerofK(n, k) * kÂ
# Function to print the resultdef printResult(n, k):Â
    print(prevPowerofK(n, k), nextPowerOfK(n, k))Â
# Driver coden = 6k = 3Â
printResult(n, k)Â
# This code is contributed by divyamohan123 |
C#
// C# implementation of the approachusing System;class GFG{Â
// Function to return the highest power// of k less than or equal to nstatic 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 nstatic int nextPowerOfK(int n, int k){Â Â Â Â return prevPowerofK(n, k) * k;}Â
// Function to print the resultstatic void printResult(int n, int k){Â Â Â Â Console.WriteLine(prevPowerofK(n, k) + " " + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â nextPowerOfK(n, k));}Â
// Driver Codepublic static void Main(String []args){Â Â Â Â int n = 25, k = 3;Â Â Â Â printResult(n, k);}}Â
// This code is contributed by gauravrajput1 |
Javascript
<script>Â
Â
// Javascript implementation of the approachÂ
// Function to return the highest power// of k less than or equal to nfunction prevPowerofK(n, k){Â Â Â Â var p = parseInt(Math.log(n) / Math.log(k));Â Â Â Â return parseInt(Math.pow(k, p));}Â
// Function to return the smallest power// of k greater than or equal to nfunction nextPowerOfK(n, k){Â Â Â Â return prevPowerofK(n, k) * k;}Â
// Function to print the resultfunction printResult(n, k){Â Â Â Â document.write(prevPowerofK(n, k)Â Â Â Â Â Â Â Â Â + " " + nextPowerOfK(n, k) + "<br>");Â Â Â Â Â Â Â Â Â Â }Â
// Driver codevar n = 25, k = 3;printResult(n, k);Â
Â
</script> |
Output:Â
9 27
Â
Time Complexity: O(logkn), as inbuilt pow function will cost O (logkn) time.
Auxiliary Space: O(1), as we are not using any extra space.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



