Floor value Kth root of a number using Recursive Binary Search

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: 3
Explanation:
Kth root of 27 = 3. Therefore 3 is the greatest whole number less than equal to Kth root of 25.

Input: N = 36, K = 3
Output: 3
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
    
  3. 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)
    
  4. 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 implmentation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
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 BS
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 Code
int main()
{
  
    // Given N and K
    int N = 16, K = 4;
  
    // Function Call
    cout << nthRootSearch(0, N, N, K)
         << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
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 BS
static 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 Code
public 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 BS
def 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 K
N = 16; K = 4;
  
# Function Call
print(nthRootSearch(0, N, N, K))
  
# This code is contributed by Code_Mech

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using 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 BS
static 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 Code
public 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

chevron_right


Output:

2

Time Complexity: O(log N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : rutvik_56, Code_Mech