binary search
class Solution { public: int sqrt(int x) { long long base = static_cast<long long>(x); long long l = 0; long long r = base / 2 + 1; while(l <= r) { long long mid = l + (r - l)/2; if(mid * mid == base) return static_cast<int>( mid); if(mid*mid < base) l = mid + 1; else r = mid - 1; } return static_cast<int>(r); } };