Check if a given number is a Perfect square using Binary Search Last Updated : 12 Jul, 2025 Comments Improve Suggest changes 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 Merge Sort - Data Structure and Algorithms Tutorials 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 DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 12 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Linked List Data Structure A linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List: 2 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ 3 min read Like