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 = 4
Input: 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++
#include <bits/stdc++.h>
using namespace std;
int prevPowerofK( int n, int k)
{
int p = ( int )( log (n) / log (k));
return ( int ) pow (k, p);
}
int nextPowerOfK( int n, int k)
{
return prevPowerofK(n, k) * k;
}
void printResult( int n, int k)
{
cout << prevPowerofK(n, k) << " " << nextPowerOfK(n, k) << endl;
}
int main()
{
int n = 25, k = 3;
printResult(n, k);
return 0;
}
|
C
#include <math.h>
#include <stdio.h>
int prevPowerofK( int n, int k)
{
int p = ( int )( log (n) / log (k));
return ( int ) pow (k, p);
}
int nextPowerOfK( int n, int k)
{
return prevPowerofK(n, k) * k;
}
void printResult( int n, int k)
{
printf ( "%d %d\n" , prevPowerofK(n, k), nextPowerOfK(n, k));
}
int main()
{
int n = 25, k = 3;
printResult(n, k);
return 0;
}
|
Java
import java.io.*;
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;
}
static void printResult( int n, int k)
{
System.out.println(prevPowerofK(n, k) + " " +
nextPowerOfK(n, k));
}
public static void main (String args[])
{
int n = 25 , k = 3 ;
printResult(n, k);
}
}
|
Python3
import math
def prevPowerofK(n, k):
p = int (math.log(n) / math.log(k))
return int (math. pow (k, p))
def nextPowerOfK(n, k):
return prevPowerofK(n, k) * k
def printResult(n, k):
print (prevPowerofK(n, k), nextPowerOfK(n, k))
n = 6
k = 3
printResult(n, k)
|
C#
using System;
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;
}
static void printResult( int n, int k)
{
Console.WriteLine(prevPowerofK(n, k) + " " +
nextPowerOfK(n, k));
}
public static void Main(String []args)
{
int n = 25, k = 3;
printResult(n, k);
}
}
|
Javascript
<script>
function prevPowerofK(n, k)
{
var p = parseInt(Math.log(n) / Math.log(k));
return parseInt(Math.pow(k, p));
}
function nextPowerOfK(n, k)
{
return prevPowerofK(n, k) * k;
}
function printResult(n, k)
{
document.write(prevPowerofK(n, k)
+ " " + nextPowerOfK(n, k) + "<br>" );
}
var n = 25, k = 3;
printResult(n, k);
</script>
|
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.
Last Updated :
18 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...