Floor value Kth root of a number using Recursive Binary Search

• Last Updated : 27 Jun, 2021

Given two numbers N and K, the task is to find the floor value of Kth root of the number N
The Floor Kth root of a number N is the greatest whole number which is less than or equal to its Kth root.
Examples:

Input: N = 27, K = 3
Output:
Explanation:
Kth root of 27 = 3. Therefore 3 is the greatest whole number less than equal to Kth root of 27.
Input: N = 36, K = 3
Output:
Explanation:
Kth root of 36 = 3.30
Therefore 3 is the greatest whole number less than equal to Kth root of 36 (3.30)

Naive Approach: The idea is to find the Kth power of numbers from 1 to N till the Kth power of some number K becomes greater than N. Then, the value of (K – 1) will be the floor value of Kth root of N
Below is the algorithm to solve this problem using Naive approach:

• Iterate a loop from numbers 1 to N in K.
• For any K, if its Kth power becomes greater than N, then K-1 is the floor value of Kth root of N.

Time Complexity: O(√N)
Efficient Approach:
From the Naive approach, it is clear that the floor value of the Kth root of N will lie in the range [1, N]. Hence instead of checking each number in this range, we can efficiently search the required number in this range by using Binary Search
Below is the recursive algorithm to solve the above problem using Binary Search

1. Implement the Binary Search in the range 0 to N.
2. Find the mid value of the range using formula:

mid = (start + end) / 2
1.
2. Base Case: The recursive call will get executed till Kth power of mid is less than or equal to N and the Kth power of (mid+1) is greater than equal to N

(midK ≤ N) and ((mid + 1)K > N)
1.
2. If the base case is not satisfied, then the range will get changed accordingly.
• If the Kth power of mid is less than equal to N, then the range gets updated to [mid + 1, end]

if(midK ≤ N)
updated range = [mid + 1, end]
•
• If the Kth power of mid is greater than N, then the range gets updated to [low, mid + 1]

if(midK > N)
updated range = [low, mid - 1]
•

Below is the implementation of the above approach:

C++

 // C++ program for the above approach#include using namespace std; // Function to calculate x raised// to the power y in O(logn)int power(int x, unsigned int y){    int temp;    if (y == 0)        return 1;    temp = power(x, y / 2);    if (y % 2 == 0)        return temp * temp;    else        return x * temp * temp;} // Function to find the Kth// root of the number N using BSint nthRootSearch(int low, int high,                  int N, int K){     // If the range is still valid    if (low <= high) {         // Find the mid-value of range        int mid = (low + high) / 2;         // Base Case        if ((power(mid, K) <= N)            && (power(mid + 1, K) > N)) {            return mid;        }         // Condition to check if the        // left search space is useless        else if (power(mid, K) < N) {            return nthRootSearch(mid + 1,                                 high, N, K);        }        else {            return nthRootSearch(low,                                 mid - 1,                                 N, K);        }    }    return low;} // Driver Codeint main(){     // Given N and K    int N = 16, K = 4;     // Function Call    cout << nthRootSearch(0, N, N, K)         << endl;    return 0;}

Java

 // Java program for the above approachclass GFG{ // Function to calculate x raised// to the power y in O(logn)static int power(int x, int y){    int temp;    if (y == 0)        return 1;             temp = power(x, y / 2);    if (y % 2 == 0)        return temp * temp;    else        return x * temp * temp;} // Function to find the Kth// root of the number N using BSstatic int nthRootSearch(int low, int high,                         int N, int K){         // If the range is still valid    if (low <= high)    {                 // Find the mid-value of range        int mid = (low + high) / 2;                 // Base Case        if ((power(mid, K) <= N) &&            (power(mid + 1, K) > N))        {            return mid;        }                 // Condition to check if the        // left search space is useless        else if (power(mid, K) < N)        {            return nthRootSearch(mid + 1,                                 high, N, K);        }        else        {            return nthRootSearch(low,                                 mid - 1, N, K);        }    }    return low;} // Driver Codepublic static void main(String s[]){         // Given N and K    int N = 16, K = 4;     // Function Call    System.out.println(nthRootSearch(0, N, N, K));}} // This code is contributed by rutvik_56

Python3

 # Python3 program for the above approach # Function to calculate x raised# to the power y in O(logn)def power(x, y):     if (y == 0):        return 1;    temp = power(x, y // 2);    if (y % 2 == 0):        return temp * temp;    else:        return x * temp * temp; # Function to find the Kth# root of the number N using BSdef nthRootSearch(low, high, N, K):     # If the range is still valid    if (low <= high):         # Find the mid-value of range        mid = (low + high) // 2;         # Base Case        if ((power(mid, K) <= N) and            (power(mid + 1, K) > N)):            return mid;         # Condition to check if the        # left search space is useless        elif (power(mid, K) < N):            return nthRootSearch(mid + 1,                                 high, N, K);        else:            return nthRootSearch(low,                                 mid - 1,                                 N, K);         return low; # Driver Code # Given N and KN = 16; K = 4; # Function Callprint(nthRootSearch(0, N, N, K)) # This code is contributed by Code_Mech

C#

 // C# program for the above approachusing System;class GFG{ // Function to calculate x raised// to the power y in O(logn)static int power(int x, int y){    int temp;    if (y == 0)        return 1;             temp = power(x, y / 2);    if (y % 2 == 0)        return temp * temp;    else        return x * temp * temp;} // Function to find the Kth// root of the number N using BSstatic int nthRootSearch(int low, int high,                         int N, int K){         // If the range is still valid    if (low <= high)    {                 // Find the mid-value of range        int mid = (low + high) / 2;                 // Base Case        if ((power(mid, K) <= N) &&            (power(mid + 1, K) > N))        {            return mid;        }                 // Condition to check if the        // left search space is useless        else if (power(mid, K) < N)        {            return nthRootSearch(mid + 1,                                 high, N, K);        }        else        {            return nthRootSearch(low,                                 mid - 1, N, K);        }    }    return low;} // Driver Codepublic static void Main(){         // Given N and K    int N = 16, K = 4;     // Function Call    Console.Write(nthRootSearch(0, N, N, K));}} // This code is contributed by Code_Mech

Javascript


Output:
2

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

My Personal Notes arrow_drop_up