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 = 3printResult(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> |
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.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
