Problem statement

https://binarysearch.com/problems/Guess-the-Root/

Solution

Equal to Leetcode 0069 Sqrt(x).

Complexity

It is O(log n) for time and O(1) for space.

Code

class Solution:
    def solve(self, x):
        if x == 0: return 0
        
        beg, end = 1, x
        while end - beg > 1:
            mid = (beg + end)//2
            if mid*mid > x:
                end = mid
            else:
                beg = mid
                
        return beg