Check if a given number is a Perfect square using Binary Search Last Updated : 25 Sep, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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; } 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 <script> // Javascript program to check if a // given number is Perfect // square using Binary Search // Function to check for // perfect square number function checkPerfectSquare(N, start, last) { // Find the mid value // from start and last let mid = parseInt((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 let N = 65; document.write(checkPerfectSquare(N, 1, N)); // This code is contributed by rishavmahato348 </script> Output: -1 Time Complexity: O(Logn)Auxiliary Space: O(Logn) for recursive stack space. Comment More infoAdvertise with us Next Article Check if a given number is a Perfect square using Binary Search E Eternity_vaibhav Follow Improve Article Tags : Divide and Conquer Searching Mathematical Recursion C++ Programs DSA Binary Search tail-recursion maths-perfect-square +5 More Practice Tags : Binary SearchDivide and ConquerMathematicalRecursionSearching +1 More Similar Reads Check if given number is perfect square Given a number n, check if it is a perfect square or not. Examples : Input : n = 36Output : YesInput : n = 2500Output : YesExplanation: 2500 is a perfect square of 50Input : n = 8Output : NoTable of ContentUsing sqrt()Using ceil, floor and sqrt() functionsUsing Binary searchUsing Mathematical Proper 13 min read Check if the square of a number is divisible by K or not Given two integers, X and K, the task is to find if X2 is divisible by K or not. Here, both K and X can lie in the range [1,1018]. Examples: Input: X = 6, K = 9 Output: YES Explanation: Since 62 is equal to 36, which is divisible by 9. Input: X = 7, K = 21 Output: NO Explanation: Since 72 is equal t 4 min read Check if a number can be represented as a sum of a Prime Number and a Perfect Square Given a positive integer N, the task is to check if N can be represented as a sum of a Prime Number and a Perfect Square or not. If it is possible to represent N in required form, then print "Yes". Otherwise, print "No". Examples: Input: N = 27Output: YesExplanation: 27 can be expressed as sum of 2 15+ min read Sum of the digits of square of the given number which has only 1's as its digits Given a number represented as string str consisting of the digit 1 only i.e. 1, 11, 111, .... The task is to find the sum of digits of the square of the given number. Examples: Input: str = 11 Output: 4 112 = 121 1 + 2 + 1 = 4 Input: str = 1111 Output: 16 Naive approach: Find the square of the given 6 min read Find square root of number upto given precision using binary search Given a positive number n and precision p, find the square root of number upto p decimal places using binary search. Note : Prerequisite : Binary search Examples: Input : number = 50, precision = 3Output : 7.071Input : number = 10, precision = 4Output : 3.1622 We have discussed how to compute the in 8 min read Like