Given a non-negative integer x,&nbsp;compute and return the square root of x.

Since the return type&nbsp;is an integer, the decimal digits are truncated, and only the integer part of the result&nbsp;is returned.

Note:&nbsp;You are not allowed to use any built-in exponent function or operator, such as pow(x, 0.5) or&nbsp;x ** 0.5.

&nbsp;
Example 1:

Input: x = 4
Output: 2


Example 2:

Input: x = 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned.

&nbsp;
Constraints:


	0 &lt;= x &lt;= 231 - 1


class Solution:
    def mySqrt(self, x: int) -> int:
        beg =0
        end =x
        while beg <=end:
            mid = (beg+end)//2
            sqr = mid*mid
            if sqr == x:
                return mid
            elif sqr < x:
                beg = mid+1
            else:
                end = mid-1
        return end
        
class Solution {
    public int mySqrt(int x) {
        long answer = 0;
        while (answer * answer <= x) {
            answer += 1;
        }
        return (int)answer - 1;
    }
}
class Solution {
public:
    int mySqrt(int x) {
        int s = 0 , e = x , mid;
        
        while(s<e)
        {
            mid = s + (e-s)/2 ;
            if(e-s == 1)
                break ;
            long double sqr = (long double)mid *mid;
            if(sqr <= x)
                s = mid ;
            else
                e = mid - 1 ;
        }
        if(e*e <= x )
            return e ;
        else
            return s ;
    }
}; ```
/**
 * @param {number} x
 * @return {number}
 */
var mySqrt = function(x) {
    const numx = x;
    let num = 0;

    while (num <= x) {
        const avg = Math.floor((num+x) / 2);
        if (avg * avg > numx) x = avg - 1;
        else num = avg + 1;
    }
    return x;
};
Sqrt(x)