Square root of a number | Practice | GeeksforGeeks

Given an integer x, find the square root of x. If x is not a perfect square, then return floor(√x).
 
Example 1:
Input:
x = 5
Output: 2
Explanation: Since, 5 is not a perfect 
square, floor of square_root of 5 is 2.

Example 2:
Input:
x = 4
Output: 2
Explanation: Since, 4 is a perfect 
square, so its square root is 2.
 
Your Task:
You don't need to read input or print anything. The task is to complete the function floorSqrt() which takes x as the input parameter and return its square root.
Note: Try Solving the question without using sqrt Function.
 
Expected Time Complexity: O(log N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 ≤ x ≤ 107
0
iam6983invinciblePremium3 days ago
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;

  

 // } Driver Code Ends
// Function to find square root
// x: element to find square root
class Solution{
  public:
    long long int floorSqrt(long long int x) 
    {
        long long int high=x;
      long long   int low=1;
      long long  int ans=-1;
        while(high>=low){
            long long int mid=(high+low)/2;
            long long int msq=mid*mid;
            if(msq==x){
                return mid;
            }
            else if(msq>x){
                high=mid-1;
            }
            else{
                low=mid+1;
                ans=mid;
            }
            
        }
        return ans;
        
        
        
    }
};

// { Driver Code Starts.

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		long long n;
		cin>>n;
		Solution obj;
		cout << obj.floorSqrt(n) << endl;
	}
    return 0;   
}
  // } Driver Code Ends
0
godhanivivekp
This comment was deleted.
-1
coolamar6265 days ago
return (long long int )sqrt(x);
 
0
hrithikjain98896 days ago
Total Time Taken:
0.01/1.02
class Solution{ public:   long long int floorSqrt(long long int x)    {       // Your code goes here      return floor(sqrt(x));          }};
0
akashmarkad22101 week ago
JAVA Solution 
 
long floorSqrt(long x) { // Your code here    // long ans = Math.sqrt(x);       // long floor = Math.floor(ans);  return (long) (Math.floor(Math.sqrt(x))); }
+1
akasksingh0802 weeks ago
long floorSqrt(long x)
	 {
		// Your code here
		long low=1;
		long high =x; 
		long ans=-1;
		while(low<=high)
		{
		    long mid = (low+high)/2;
		    long mSq =mid*mid;
		    if(mSq==x)
		    {
		        return mid;
		    }
		    else if(mSq>x)
		    {
		        high=mid-1;
		    }
		    else
		    {
		        low=mid+1;
		        ans= mid;
		    }
		}
		return ans;
	 }
-2
ajay7yadav952 weeks ago
// hello JavaScript lovers ------------------→ A7
 
let res = Math.floor(Math.sqrt(x));       return res;
-1
nischithtn2 weeks ago
long floorSqrt(long x) { // Your code here long p = (long)Math.sqrt(x);   return p; }
-1
abhinavkrbxr20042 weeks ago
#include <stdio.h>
#include <math.h>
Int main() 
{
Int a, root;
Printf("Enter a number:") ;
scanf("%d", &a) ;
root=sqrt(a) ;
Printf ("The square root of the given number is %d", root) ;
return 0;
}
-1
giridharsonu1232 weeks ago
JavaScript Solution
for(let i = 1; i <= x; i++)  {          if(i * i === x){          return i;      }      else if (i * i >= x) {          return i-1;      }      else if (x === 2) {          return 1;      }      }  
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.