Square root of an integer | Returns floor of square root of x ; Base cases ; Do Binary Search for floor ( sqrt ( x ) ) ; If x is a perfect square ; Since we need floor , we update answer when mid * mid is smaller than x , and move closer to sqrt ( x ) ; If mid * mid is greater than x ; Driver Code
function floorSqrt ( x ) { if ( x == 0 x == 1 ) return x ; let start = 1 ; let end = x ; let ans ; while ( start <= end ) { let mid = ( start + end ) / 2 ; if ( mid * mid == x ) return mid ; if ( mid * mid < x ) { start = mid + 1 ; ans = mid ; } else end = mid - 1 ; } return ans ; } let x = 11 ; document . write ( floorSqrt ( x ) + " " ) ;