Math Fundamentals18 sections · 814 units
Open in Course

Practice - Sqrt(x)

LeetCode 69

Given a non-negative integer xx, return the square root of xx rounded down to the nearest integer. You cannot use any built-in exponent function or operator.

For example, if x=8x = 8, return 22 because 8=2.828...\sqrt{8} = 2.828..., which rounds down to 22.

Hint: use binary search to find the largest integer kk such that k2xk^2 \leq x. This is similar to the perfect square problem.