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++
// C++ program to check if a // given number is Perfect // square using Binary Search #include <iostream> 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; } |
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 = 65print (checkPerfectSquare(N, 1, N)) # This code is contributed by PratikBasu |
-1
Time Complexity: O(Logn)
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
- Check if a number is perfect square without finding square root
- Check if given number is perfect square
- Check whether the number can be made perfect square after adding K
- Check whether the number can be made perfect square after adding 1
- Find smallest perfect square number A such that N + A is also a perfect square number
- Find square root of number upto given precision using binary search
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Check perfect square using addition/subtraction
- Check if product of array containing prime numbers is a perfect square
- Largest number that is not a perfect square
- Find minimum number to be divided to make a number a perfect square
- Number of times the largest perfect square number can be subtracted from N
- Previous perfect square and cube number smaller than number N
- Largest perfect square number in an Array
- Find the Next perfect square greater than a given number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : PratikBasu

