Skip to content

visual walkthrough

Sqrt(x)

EasyBinary Search on AnswerReported at: AmazonAppleGoogle+4

Searching over the answer space instead of an array.

Solve on LeetCode

The idea

There's no array here, but the answers 0, 1, 2, …, x are "sorted": k² ≤ x is true for small k and false after some point.

Binary search over those answers for the last k that fits.

Complexity

approachtimespace
Count upO(√x)O(1)
Binary search on the answerO(log x)O(1)

More walkthroughs