📜  使用二分搜索检查给定数字是否为完全平方数

📅  最后修改于: 2021-09-16 11:17:38             🧑  作者: Mango

检查给定的数字N是否是完全平方数。如果是,则返回它是完全平方数的数字,否则打印 -1。

例子:

方法:为了解决上面提到的问题,我们将使用二分搜索算法。

  • 从起始值和最后一个值中找出 mid 元素,并将 mid(mid*mid) 的平方的值与 N 进行比较。
  • 如果相等,则返回 mid 否则检查 square(mid*mid) 是否大于 N,然后使用相同的起始值递归调用但最后更改为 mid-1 值,并且如果 square(mid*mid)小于然后使用相同的最后一个值递归调用N ,但更改了起始值。
  • 如果 N 不是平方根,则返回 -1。

下面是上述方法的实现:

C++
// C++ program to check if a
// given number is Perfect
// square using Binary Search
 
#include 
using namespace std;
 
// function to check for
// perfect square number
int checkPerfectSquare(
    long int N,
    long int start,
    long int last)
{
    // Find the mid value
    // from start and last
    long int mid = (start + last) / 2;
 
    if (start > last) {
        return -1;
    }
 
    // check if we got the number which
    // is square root of the perfect
    // square number N
    if (mid * mid == N) {
        return mid;
    }
 
    // if the square(mid) is greater than N
    // it means only lower values then mid
    // will be possibly the square root of N
    else if (mid * mid > N) {
        return checkPerfectSquare(
            N, start, mid - 1);
    }
 
    // if the square(mid) is less than N
    // it means only higher values then mid
    // will be possibly the square root of N
    else {
        return checkPerfectSquare(
            N, mid + 1, last);
    }
}
 
// Driver code
int main()
{
    long int N = 65;
 
    cout << checkPerfectSquare(N, 1, N);
    return 0;
}


Java
// Java program to check if a
// given number is Perfect
// square using Binary Search
import java.util.*;
 
class GFG {
 
// Function to check for
// perfect square number
static int checkPerfectSquare(long N,
                              long start,
                              long last)
{
    // Find the mid value
    // from start and last
    long mid = (start + last) / 2;
 
    if (start > last)
    {
        return -1;
    }
 
    // Check if we got the number which
    // is square root of the perfect
    // square number N
    if (mid * mid == N)
    {
        return (int)mid;
    }
 
    // If the square(mid) is greater than N
    // it means only lower values then mid
    // will be possibly the square root of N
    else if (mid * mid > N)
    {
        return checkPerfectSquare(N, start,
                                  mid - 1);
    }
 
    // If the square(mid) is less than N
    // it means only higher values then mid
    // will be possibly the square root of N
    else
    {
        return checkPerfectSquare(N, mid + 1,
                                  last);
    }
}
 
// Driver code
public static void main(String[] args)
{
    long N = 65;
    System.out.println(checkPerfectSquare(N, 1, N));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to check if a
# given number is perfect
# square using Binary Search
 
# Function to check for
# perfect square number
def checkPerfectSquare(N, start, last):
 
    # Find the mid value
    # from start and last
    mid = int((start + last) / 2)
 
    if (start > last):
        return -1
 
    # Check if we got the number which
    # is square root of the perfect
    # square number N
    if (mid * mid == N):
        return mid
 
    # If the square(mid) is greater than N
    # it means only lower values then mid
    # will be possibly the square root of N
    elif (mid * mid > N):
        return checkPerfectSquare(N, start,
                                  mid - 1)
 
    # If the square(mid) is less than N
    # it means only higher values then mid
    # will be possibly the square root of N
    else:
        return checkPerfectSquare(N, mid + 1,
                                  last)
 
# Driver code
N = 65
print (checkPerfectSquare(N, 1, N))
 
# This code is contributed by PratikBasu


C#
// C# program to check if a
// given number is Perfect
// square using Binary Search
using System;
 
class GFG{
 
// Function to check for
// perfect square number
public static int checkPerfectSquare(int N,
                                     int start,
                                     int last)
{
    // Find the mid value
    // from start and last
    int mid = (start + last) / 2;
 
    if (start > last)
    {
        return -1;
    }
 
    // Check if we got the number which
    // is square root of the perfect
    // square number N
    if (mid * mid == N)
    {
        return mid;
    }
 
    // If the square(mid) is greater than N
    // it means only lower values then mid
    // will be possibly the square root of N
    else if (mid * mid > N)
    {
        return checkPerfectSquare(N, start,
                                  mid - 1);
    }
 
    // If the square(mid) is less than N
    // it means only higher values then mid
    // will be possibly the square root of N
    else
    {
        return checkPerfectSquare(N, mid + 1,
                                  last);
    }
}
 
// Driver code
public static int Main()
{
    int N = 65;
 
    Console.Write(checkPerfectSquare(N, 1, N));
    return 0;
}
}
 
// This code is contributed by sayesha


Javascript


输出:
-1

时间复杂度: O(Logn)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程