Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Find the power of K nearest to N

  • Last Updated : 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 = 3
Output: 3
Explanation: 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 = 7
Output: 49
Explanation: The powers of 7 are 7, 49, 343, . . . 49 is the closest to 32 among these numbers.

Input: N = 6, K = 3
Output: 9
Explanation: Both 3 and 9 have distance = 3. But 9 is larger between 3 and 9.

 

Approach: Follow the below steps to solve this problem:

  1. For the number N, find the nearest powers of K greater and smaller.
  2. 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]
  3. 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]
  4. 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++




// C++ program to
// implement the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find nearest power
int 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 Code
int main()
{
    int N = 32, K = 7;
    cout << nearestPowerOfK(N, K);
    return 0;
}

Java




// Java implementation for the above approach
import java.util.*;
public class GFG
{
 
// Function to find nearest power
static 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 Code
public 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




# Python3 program to implement the above approach
import math
 
# Function to find nearest power
def 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 Code
if __name__ == "__main__":
 
    N = 32
    K = 7
     
    print(nearestPowerOfK(N, K))
 
# This code is contributed by rakeshsahni

C#




// C# implementation for the above approach
using System;
class GFG
{
 
// Function to find nearest power
static 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 Code
public static void Main()
{
    int N = 32, K = 7;
 
    Console.Write(nearestPowerOfK(N, K));
}
}
 
// This code is contributed by Samim Hossain Mondal.

Javascript




<script>
 
// JavaScript program to implement
// the above approach
 
// Function to find nearest power
function 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 Code
let N = 32, K = 7;
 
document.write(nearestPowerOfK(N, K));
 
// This code is contributed by Potta Lokesh
 
</script>
Output
49

Time Complexity: O(1)
Auxiliary Space: O(1)

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!