Given an integer x, find it’s square root. If x is not a perfect square, then return floor(√x).
Examples :
Input: x = 4 Output: 2 Explanation: The square root of 4 is 2. Input: x = 11 Output: 3 Explanation: The square root of 11 lies in between 3 and 4 so floor of the square root is 3.
There can be many ways to solve this problem. For example Babylonian Method is one way.
Simple Approach: To find the floor of the square root, try with all-natural numbers starting from 1. Continue incrementing the number until the square of that number is greater than the given number.
- Algorithm:
- Create a variable (counter) i and take care of some base cases, i.e when the given number is 0 or 1.
- Run a loop until i*i <= n , where n is the given number. Increment i by 1.
- The floor of the square root of the number is i – 1
-
Implementation:
C++
// A C++ program to find floor(sqrt(x)#include<bits/stdc++.h>usingnamespacestd;// Returns floor of square root of xintfloorSqrt(intx){// Base casesif(x == 0 || x == 1)returnx;// Staring from 1, try all numbers until// i*i is greater than or equal to x.inti = 1, result = 1;while(result <= x){i++;result = i * i;}returni - 1;}// Driver programintmain(){intx = 11;cout << floorSqrt(x) << endl;return0;}chevron_rightfilter_noneJava
// A Java program to find floor(sqrt(x))classGFG {// Returns floor of square root of xstaticintfloorSqrt(intx){// Base casesif(x ==0|| x ==1)returnx;// Staring from 1, try all numbers until// i*i is greater than or equal to x.inti =1, result =1;while(result <= x) {i++;result = i * i;}returni -1;}// Driver programpublicstaticvoidmain(String[] args){intx =11;System.out.print(floorSqrt(x));}}// This code is contributed by Smitha Dinesh Semwal.chevron_rightfilter_nonePython3
# Python3 program to find floor(sqrt(x)# Returns floor of square root of xdeffloorSqrt(x):# Base casesif(x==0orx==1):returnx# Staring from 1, try all numbers until# i*i is greater than or equal to x.i=1; result=1while(result <=x):i+=1result=i*ireturni-1# Driver Codex=11print(floorSqrt(x))# This code is contributed by Smitha Dinesh Semwal.chevron_rightfilter_noneC#
// A C# program to// find floor(sqrt(x))usingSystem;classGFG{// Returns floor of// square root of xstaticintfloorSqrt(intx){// Base casesif(x == 0 || x == 1)returnx;// Staring from 1, try all// numbers until i*i is// greater than or equal to x.inti = 1, result = 1;while(result <= x){i++;result = i * i;}returni - 1;}// Driver CodestaticpublicvoidMain (){intx = 11;Console.WriteLine(floorSqrt(x));}}// This code is contributed by ajitchevron_rightfilter_nonePHP
<?php// A PHP program to find floor(sqrt(x)// Returns floor of square root of xfunctionfloorSqrt($x){// Base casesif($x== 0 ||$x== 1)return$x;// Staring from 1, try all// numbers until i*i is// greater than or equal to x.$i= 1;$result= 1;while($result<=$x){$i++;$result=$i*$i;}return$i- 1;}// Driver Code$x= 11;echofloorSqrt($x),"\n";// This code is contributed by ajit?>chevron_rightfilter_none
Output :3
-
Complexity Analysis:
- Time Complexity: O(√ n).
Only one traversal of the solution is needed, so the time complexity is O(√ n). - Space Complexity: O(1).
Constant extra space is needed.
- Time Complexity: O(√ n).
Thanks Fattepur Mahesh for suggesting this solution.
Better Approach: The idea is to find the largest integer i whose square is less than or equal to the given number. The idea is to use Binary Search to solve the problem. The values of i * i is monotonically increasing, so the problem can be solved using binary search.
- Algorithm:
- Take care of some base cases, i.e when the given number is 0 or 1.
- Create some variables, lowerbound l = 0, upperbound r = n, where n is the given number, mid and ans to store the answer.
- Run a loop until l <= r , the search space vanishes
- Check if the square of mid (mid = (l + r)/2 ) is less than or equal to n, If yes then search for a larger value in second half oF search space, i.e l = mid + 1, update ans = mid
- Else if the square of mid is less than n then search for a smaller value in first half oF search space, i.e r = mid – 1
- Print the value of answer ( ans)
-
Implementation:
C/C++
// A C++ program to find floor(sqrt(x)#include<bits/stdc++.h>usingnamespacestd;// Returns floor of square root of xintfloorSqrt(intx){// Base casesif(x == 0 || x == 1)returnx;// Do Binary Search for floor(sqrt(x))intstart = 1, end = x, ans;while(start <= end){intmid = (start + end) / 2;// If x is a perfect squareif(mid*mid == x)returnmid;// Since we need floor, we update answer when mid*mid is// smaller than x, and move closer to sqrt(x)if(mid*mid < x){start = mid + 1;ans = mid;}else// If mid*mid is greater than xend = mid-1;}returnans;}// Driver programintmain(){intx = 11;cout << floorSqrt(x) << endl;return0;}chevron_rightfilter_noneJava
// A Java program to find floor(sqrt(x)publicclassTest{publicstaticintfloorSqrt(intx){// Base Casesif(x ==0|| x ==1)returnx;// Do Binary Search for floor(sqrt(x))intstart =1, end = x, ans=0;while(start <= end){intmid = (start + end) /2;// If x is a perfect squareif(mid*mid == x)returnmid;// Since we need floor, we update answer when mid*mid is// smaller than x, and move closer to sqrt(x)if(mid*mid < x){start = mid +1;ans = mid;}else// If mid*mid is greater than xend = mid-1;}returnans;}// Driver Methodpublicstaticvoidmain(String args[]){intx =11;System.out.println(floorSqrt(x));}}// Contributed by InnerPeacechevron_rightfilter_nonePython3
# Python 3 program to find floor(sqrt(x)# Returns floor of square root of xdeffloorSqrt(x) :# Base casesif(x==0orx==1) :returnx# Do Binary Search for floor(sqrt(x))start=1end=xwhile(start <=end) :mid=(start+end)//2# If x is a perfect squareif(mid*mid==x) :returnmid# Since we need floor, we update# answer when mid*mid is smaller# than x, and move closer to sqrt(x)if(mid*mid < x) :start=mid+1ans=midelse:# If mid*mid is greater than xend=mid-1returnans# driver codex=11print(floorSqrt(x))# This code is contributed by Nikita Tiwari.chevron_rightfilter_noneC#
// A C# program to// find floor(sqrt(x)usingSystem;classGFG{publicstaticintfloorSqrt(intx){// Base Casesif(x == 0 || x == 1)returnx;// Do Binary Search// for floor(sqrt(x))intstart = 1, end = x, ans = 0;while(start <= end){intmid = (start + end) / 2;// If x is a// perfect squareif(mid * mid == x)returnmid;// Since we need floor, we// update answer when mid *// mid is smaller than x,// and move closer to sqrt(x)if(mid * mid < x){start = mid + 1;ans = mid;}// If mid*mid is// greater than xelseend = mid-1;}returnans;}// Driver CodestaticpublicvoidMain (){intx = 11;Console.WriteLine(floorSqrt(x));}}// This code is Contributed by m_kitchevron_rightfilter_nonePHP
<?php// A PHP program to find floor(sqrt(x)// Returns floor of// square root of xfunctionfloorSqrt($x){// Base casesif($x== 0 ||$x== 1)return$x;// Do Binary Search// for floor(sqrt(x))$start= 1;$end=$x;$ans;while($start<=$end){$mid= ($start+$end) / 2;// If x is a perfect squareif($mid*$mid==$x)return$mid;// Since we need floor, we update// answer when mid*mid is smaller// than x, and move closer to sqrt(x)if($mid*$mid<$x){$start=$mid+ 1;$ans=$mid;}// If mid*mid is// greater than xelse$end=$mid-1;}return$ans;}// Driver Code$x= 11;echofloorSqrt($x),"\n";// This code is contributed by ajit?>chevron_rightfilter_none
Output :3
-
Complexity Analysis:
- Time complexity: O(log n).
The time complexity of binary search is O(log n). - Space Complexity: O(1).
Constant extra space is needed.
- Time complexity: O(log n).
Thanks to Gaurav Ahirwar for suggesting above method.
Note: The Binary Search can be further optimized to start with ‘start’ = 0 and ‘end’ = x/2. Floor of square root of x cannot be more than x/2 when x > 1.
Thanks to vinit for suggesting above optimization.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

