Suppose we have a non-negative number n, we have to find a number r such that r * r = n and we have to round down to the nearest integer. We have to solve this problem without using the builtin square-root function.
So, if the input is like 1025, then the output will be 32.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, n): if n <= 1: return n start, end = 1, n while start < end: mid = start + end >> 1 if mid * mid <= n: start = mid + 1 else: end = mid return start - 1 ob = Solution() print(ob.solve(1025))