Check if a given number is a Perfect square using Binary Search
Check if a given number N is a perfect square or not. If yes then return the number of which it is a perfect square, Else print -1.
Examples:
Input: N = 4900
Output 70
Explanation:
4900 is a perfect square number of 70 because 70 * 70 = 4900
Input: N = 81
Output: 9
Explanation:
81 is a perfect square number of 9 because 9 * 9 = 81
Approach: To solve the problem mentioned above we will use the Binary Search Algorithm.
- Find the mid element from the start and last value and compare the value of the square of mid(mid*mid) with N.
- If it is equal then return the mid otherwise check if the square(mid*mid) is greater than N then recursive call with the same start value but changed last to mid-1 value and if the square(mid*mid) is less than the N then recursive call with the same last value but changed start value.
- If the N is not a square root then return -1.
Below is the implementation of above approach:
C++
#include <iostream>
using namespace std;
int checkPerfectSquare(
long int N,
long int start,
long int last)
{
long int mid = (start + last) / 2;
if (start > last) {
return -1;
}
if (mid * mid == N) {
return mid;
}
else if (mid * mid > N) {
return checkPerfectSquare(
N, start, mid - 1);
}
else {
return checkPerfectSquare(
N, mid + 1, last);
}
}
int main()
{
long int N = 65;
cout << checkPerfectSquare(N, 1, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int checkPerfectSquare( long N,
long start,
long last)
{
long mid = (start + last) / 2 ;
if (start > last)
{
return - 1 ;
}
if (mid * mid == N)
{
return ( int )mid;
}
else if (mid * mid > N)
{
return checkPerfectSquare(N, start,
mid - 1 );
}
else
{
return checkPerfectSquare(N, mid + 1 ,
last);
}
}
public static void main(String[] args)
{
long N = 65 ;
System.out.println(checkPerfectSquare(N, 1 , N));
}
}
|
Python3
def checkPerfectSquare(N, start, last):
mid = int ((start + last) / 2 )
if (start > last):
return - 1
if (mid * mid = = N):
return mid
elif (mid * mid > N):
return checkPerfectSquare(N, start,
mid - 1 )
else :
return checkPerfectSquare(N, mid + 1 ,
last)
N = 65
print (checkPerfectSquare(N, 1 , N))
|
C#
using System;
class GFG{
public static int checkPerfectSquare( int N,
int start,
int last)
{
int mid = (start + last) / 2;
if (start > last)
{
return -1;
}
if (mid * mid == N)
{
return mid;
}
else if (mid * mid > N)
{
return checkPerfectSquare(N, start,
mid - 1);
}
else
{
return checkPerfectSquare(N, mid + 1,
last);
}
}
public static int Main()
{
int N = 65;
Console.Write(checkPerfectSquare(N, 1, N));
return 0;
}
}
|
Javascript
<script>
function checkPerfectSquare(N, start, last)
{
let mid = parseInt((start + last) / 2);
if (start > last)
{
return -1;
}
if (mid * mid == N)
{
return mid;
}
else if (mid * mid > N)
{
return checkPerfectSquare(
N, start, mid - 1);
}
else
{
return checkPerfectSquare(
N, mid + 1, last);
}
}
let N = 65;
document.write(checkPerfectSquare(N, 1, N));
</script>
|
Time Complexity: O(Logn)
Auxiliary Space: O(Logn) for recursive stack space.
Last Updated :
25 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...