📜  使用递归二进制搜索的底值Kth根数

📅  最后修改于: 2021-04-29 12:30:22             🧑  作者: Mango

给定两个数字NK ,任务是找到数字N第K个根的底值。
数字N的底数K根是最大整数,小于或等于其K根。
例子:

原始的方法:我们的想法是找到号码的k电力从1到N,直到一些数K变得大于N的k电力。然后, (K – 1)的值将是N第K个根的底值。
以下是使用Naive方法解决此问题的算法:

  • 从K中的数字1到N循环循环。
  • 对于任何K,如果其Kth幂大于N,则K-1是N的Kth根的底值。

时间复杂度: O(√N)
高效方法:
从天真的方法来看,很明显N第K个根的底值将在[1,N]范围内。因此,我们无需检查此范围内的每个数字,而是可以通过使用二进制搜索有效地搜索此范围内的所需数字
以下是使用Binary Search解决上述问题的递归算法:

  1. 实施二进制搜索,范围为0到N。
  2. 使用公式找到范围的中间值:
mid = (start + end) / 2
  1. 基本情况:递归调用将得到执行,直到中旬k电力是小于或等于N,k电力(MID + 1)大于等于N越大。
(midK ≤ N) and ((mid + 1)K > N)
  1. 如果不满足基本要求,则范围将相应更改。
    • 如果midK次方小于N ,则范围更新为[mid + 1,end]
if(midK ≤ N)
    updated range = [mid + 1, end]
  • 如果midKth次方大于N ,则范围将更新为[low,mid + 1]
if(midK > N)
    updated range = [low, mid - 1]

以下是上述方法的实现:

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


输出:
2

时间复杂度: O(log N)