Open In App

Floor value Kth root of a number using Recursive Binary Search

Last Updated : 30 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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
Step by step algorithm for the approach:

  • Initialize a variable i with 1
  • Increment i until i^K >= N
  • If i^K == N, return i
  • Otherwise, return i-1

Below is the code for the above approach:

C++

#include <iostream>
#include <cmath>
 
using namespace std;
 
// Function to find the Kth root of N
int nthRoot(int N, int K)
{
    int i;
    for (i = 1; i < N; i++) {
        if (pow(i, K) >= N)
            break;
    }
 
    if (pow(i, K) == N)
        return i;
 
    return i - 1;
}
 
// Driver code
int main()
{
    int N = 16, K = 4;
 
    cout << nthRoot(N, K) << endl;
 
    return 0;
}

                    

Java

// Java code for the above approach
import java.util.*;
 
public class GFG {
    // Function to find the Kth root of N
    static int nthRoot(int N, int K)
    {
        int i;
        for (i = 1; i < N; i++) {
            if (Math.pow(i, K) >= N)
                break;
        }
 
        if (Math.pow(i, K) == N)
            return i;
 
        return i - 1;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int N = 16, K = 4;
 
        System.out.println(nthRoot(N, K));
    }
}
 
// This code is contributed by Susobhan Akhuli

                    

Python3

# Python code for the above approach
import math
 
# Function to find the Kth root of N
def nthRoot(N, K):
    i = 1
    while i < N:
        if pow(i, K) >= N:
            break
        i += 1
 
    if pow(i, K) == N:
        return i
 
    return i - 1
 
# Driver code
if __name__ == "__main__":
    N = 16
    K = 4
 
    print(nthRoot(N, K))
 
# This code is contributed by Susobhan Akhuli

                    

C#

using System;
 
class Program
{
    // Function to find the Kth root of N
    static int NthRoot(int N, int K)
    {
        int i;
        for (i = 1; i < N; i++)
        {
            if (Math.Pow(i, K) >= N)
                break;
        }
 
        if (Math.Pow(i, K) == N)
            return i;
 
        return i - 1;
    }
 
    static void Main()
    {
        int N = 16, K = 4;
 
        int result = NthRoot(N, K);
 
        Console.WriteLine(result);
 
        // Output the Kth root of N
    }
}

                    

Javascript

<script>
// JavaScript code for the above approach
 
// Function to find the Kth root of N
function nthRoot(N, K) {
    let i;
    for (i = 1; i < N; i++) {
        if (Math.pow(i, K) >= N)
            break;
    }
 
    if (Math.pow(i, K) === N)
        return i;
 
    return i - 1;
}
 
// Driver code
const N = 16, K = 4;
document.write(nthRoot(N, K));
 
// This code is contributed by Susobhan Akhuli
</script>

                    

Output
2




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

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 implementation of the above approach: 

C++

// 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;
}

                    

Java

// 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

                    

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 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

                    

C#

// 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

                    

Javascript

<script>
 
// JavaScript program to implement
// the above approach
  
// Function to calculate x raised
// to the power y in O(logn)
function power(x, y)
{
    let temp;
    if (y == 0)
        return 1;
          
    temp = power(x, Math.floor(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
function nthRootSearch(low, high,
                         N, K)
{
      
    // If the range is still valid
    if (low <= high)
    {
          
        // Find the mid-value of range
        let mid = Math.floor((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
 
    // Given N and K
    let N = 16, K = 4;
  
    // Function Call
    document.write(nthRootSearch(0, N, N, K));
  
 // This code is contributed by sanjoy_62.
</script>

                    

Output
2




Time Complexity: O(log N)
Auxiliary Space: O(logN) for recursive stack space.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads