Floor value Kth root of a number using Recursive Binary Search
Last Updated :
30 Nov, 2023
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 27.
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.
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;
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;
}
int main()
{
int N = 16, K = 4;
cout << nthRoot(N, K) << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
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 ;
}
public static void main(String[] args)
{
int N = 16 , K = 4 ;
System.out.println(nthRoot(N, K));
}
}
|
Python3
import math
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
if __name__ = = "__main__" :
N = 16
K = 4
print (nthRoot(N, K))
|
C#
using System;
class Program
{
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);
}
}
|
Javascript
<script>
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;
}
const N = 16, K = 4;
document.write(nthRoot(N, K));
</script>
|
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:
- Implement the Binary Search in the range 0 to N.
- Find the mid value of the range using formula:
- 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.
- 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 the Kth power of mid is greater than N, then the range gets updated to [low, mid + 1]
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
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;
}
int nthRootSearch( int low, int high,
int N, int K)
{
if (low <= high) {
int mid = (low + high) / 2;
if ((power(mid, K) <= N)
&& (power(mid + 1, K) > N)) {
return mid;
}
else if (power(mid, K) < N) {
return nthRootSearch(mid + 1,
high, N, K);
}
else {
return nthRootSearch(low,
mid - 1,
N, K);
}
}
return low;
}
int main()
{
int N = 16, K = 4;
cout << nthRootSearch(0, N, N, K)
<< endl;
return 0;
}
|
Java
class GFG{
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;
}
static int nthRootSearch( int low, int high,
int N, int K)
{
if (low <= high)
{
int mid = (low + high) / 2 ;
if ((power(mid, K) <= N) &&
(power(mid + 1 , K) > N))
{
return mid;
}
else if (power(mid, K) < N)
{
return nthRootSearch(mid + 1 ,
high, N, K);
}
else
{
return nthRootSearch(low,
mid - 1 , N, K);
}
}
return low;
}
public static void main(String s[])
{
int N = 16 , K = 4 ;
System.out.println(nthRootSearch( 0 , N, N, K));
}
}
|
Python3
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;
def nthRootSearch(low, high, N, K):
if (low < = high):
mid = (low + high) / / 2 ;
if ((power(mid, K) < = N) and
(power(mid + 1 , K) > N)):
return mid;
elif (power(mid, K) < N):
return nthRootSearch(mid + 1 ,
high, N, K);
else :
return nthRootSearch(low,
mid - 1 ,
N, K);
return low;
N = 16 ; K = 4 ;
print (nthRootSearch( 0 , N, N, K))
|
C#
using System;
class GFG{
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;
}
static int nthRootSearch( int low, int high,
int N, int K)
{
if (low <= high)
{
int mid = (low + high) / 2;
if ((power(mid, K) <= N) &&
(power(mid + 1, K) > N))
{
return mid;
}
else if (power(mid, K) < N)
{
return nthRootSearch(mid + 1,
high, N, K);
}
else
{
return nthRootSearch(low,
mid - 1, N, K);
}
}
return low;
}
public static void Main()
{
int N = 16, K = 4;
Console.Write(nthRootSearch(0, N, N, K));
}
}
|
Javascript
<script>
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 nthRootSearch(low, high,
N, K)
{
if (low <= high)
{
let mid = Math.floor((low + high) / 2);
if ((power(mid, K) <= N) &&
(power(mid + 1, K) > N))
{
return mid;
}
else if (power(mid, K) < N)
{
return nthRootSearch(mid + 1,
high, N, K);
}
else
{
return nthRootSearch(low,
mid - 1, N, K);
}
}
return low;
}
let N = 16, K = 4;
document.write(nthRootSearch(0, N, N, K));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(logN) for recursive stack space.
Share your thoughts in the comments
Please Login to comment...