Find the power of K nearest to N - GeeksforGeeks

08 Dec, 2021
Given two integers N & K. The task is to find the nearest power of K for the integer N. If there are two nearest powers, consider the larger one.
Examples:
Input: N = 5, K = 3Output: 3Explanation: The powers of 3 are 3, 9, 27, . . . Among these 3 is the nearest to 5 as it has a distance of 2.
Input: N = 32, K = 7Output: 49Explanation: The powers of 7 are 7, 49, 343, . . . 49 is the closest to 32 among these numbers.
Input: N = 6, K = 3Output: 9Explanation: Both 3 and 9 have distance = 3. But 9 is larger between 3 and 9.
Approach: Follow the below steps to solve this problem:
For the number N, find the nearest powers of K greater and smaller.The smaller power of K will be the floor value (say X) of logKN. So the value will be pow(K, X). [floor value of P = closest integer to P which is ≤ P]And greater power of K will be the ceiling value (say Y) of logKN. So the value will be pow(K, Y). [ceiling value of P = closest integer to P which is ≥ P]Calculate the difference of these two values from N and print the nearest as specified in the problem statement.
For the number N, find the nearest powers of K greater and smaller.
The smaller power of K will be the floor value (say X) of logKN. So the value will be pow(K, X). [floor value of P = closest integer to P which is ≤ P]
And greater power of K will be the ceiling value (say Y) of logKN. So the value will be pow(K, Y). [ceiling value of P = closest integer to P which is ≥ P]
Calculate the difference of these two values from N and print the nearest as specified in the problem statement.
Below is the implementation of the above approach.
C++
Java
Python3
C#
Javascript
// C++ program to// implement the above approach#include <bits/stdc++.h>using namespace std; // Function to find nearest powerint nearestPowerOfK(int N, int K){    // Finding log of the element    int lg = log(N) / log(K);     // Calculating the two possible    // nearest values    int a = pow(K, lg);    int b = pow(K, lg + 1);     // Finding the closest one    if ((N - a) < (b - N))        return a;    return b;} // Driver Codeint main(){    int N = 32, K = 7;    cout << nearestPowerOfK(N, K);    return 0;}
// Java implementation for the above approachimport java.util.*;public class GFG{ // Function to find nearest powerstatic int nearestPowerOfK(int N, int K){    // Finding log of the element    int lg = (int)(Math.log(N) / Math.log(K));     // Calculating the two possible    // nearest values    int a = (int)Math.pow(K, lg);    int b = (int)Math.pow(K, lg + 1);     // Finding the closest one    if ((N - a) < (b - N))        return a;    return b;} // Driver Codepublic static void main(String args[]){    int N = 32, K = 7;     System.out.println(nearestPowerOfK(N, K));}} // This code is contributed by Samim Hossain Mondal.
# Python3 program to implement the above approachimport math # Function to find nearest powerdef nearestPowerOfK(N, K):         # Finding log of the element    lg = math.log(N) // math.log(K)     # Calculating the two possible    # nearest values    a = int(pow(K, lg))    b = int(pow(K, lg + 1))     # Finding the closest one    if ((N - a) < (b - N)):        return a             return b # Driver Codeif __name__ == "__main__":     N = 32    K = 7         print(nearestPowerOfK(N, K)) # This code is contributed by rakeshsahni
// C# implementation for the above approachusing System;class GFG{ // Function to find nearest powerstatic int nearestPowerOfK(int N, int K){    // Finding log of the element    int lg = (int)(Math.Log(N) / Math.Log(K));     // Calculating the two possible    // nearest values    int a = (int)Math.Pow(K, lg);    int b = (int)Math.Pow(K, lg + 1);     // Finding the closest one    if ((N - a) < (b - N))        return a;    return b;} // Driver Codepublic static void Main(){    int N = 32, K = 7;     Console.Write(nearestPowerOfK(N, K));}} // This code is contributed by Samim Hossain Mondal.
<script> // JavaScript program to implement// the above approach // Function to find nearest powerfunction nearestPowerOfK(N, K){         // Finding log of the element    let lg = Math.floor(Math.log(N) /                        Math.log(K));     // Calculating the two possible    // nearest values    let a = Math.pow(K, lg);    let b = Math.pow(K, lg + 1);     // Finding the closest one    if ((N - a) < (b - N))        return a;             return b;} // Driver Codelet N = 32, K = 7; document.write(nearestPowerOfK(N, K)); // This code is contributed by Potta Lokesh </script>
49
Time Complexity: O(1)Auxiliary Space: O(1)
 
lokeshpotta20
samim2000
rakeshsahni
maths-power
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Count all possible paths from top left to bottom right of a mXn matrix
Modular multiplicative inverse
Fizz Buzz Implementation
Program to multiply two matrices
Check if a number is Palindrome
Count ways to reach the n'th stair
Merge two sorted arrays with O(1) extra space
Generate all permutation of a set in Python