Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmFloor value Kth root of a number using Recursive Binary Search

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


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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments