Implement int sqrt(int x)

    public int sqrt(int x) {
        if(x < 2){
            return x;
        }
        long i = 1, j = x - 1;
        while(i + 1 < j){
            long mid = i + (j - i) / 2;
            if(mid * mid > x){
                j = mid;
            } else{
                i = mid;
            }
        }
        if(j * j <= x){

            return (int) j;
        }
        return (int) i;
    }

.

results matching ""

    No results matching ""