Sqrt(x)
You get a non-negative integer x. Return its square root rounded down, without a built-in power or root function. Our example: x = 20 — the answer is 4: 4 · 4 = 16 fits under 20, 5 · 5 = 25 does not.
- input
x: a non-negative integer.- returns
- The square root of
xrounded down to an integer. LeetCode wants it without a built-in power function or operator such aspow(x, 0.5)orx ** 0.5. - constraints
- 0 ≤ x ≤ 2³¹ − 1
- examples
- Open a test in the tests panel: it shows the input and the expected answer.
/**
* @param {number} x
* @return {number}
*/
function mySqrt(x) {
}
Ctrl ↵
format
reset
saved in this browser
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more binary search problems
- 704Binary SearchEasysolve
- 35Search Insert PositionEasysolve
- 153Find Minimum in Rotated Sorted ArrayMediumsolve
- 33Search in Rotated Sorted ArrayMediumsolve
- 875Koko Eating BananasMediumsolve
- 1011Capacity To Ship Packages Within D DaysMediumsolve
- 274H-IndexMediumsolve
- 74Search a 2D MatrixMediumsolve
- 162Find Peak ElementMediumsolve
- 34Find First and Last Position of Element in Sorted ArrayMediumsolve
- 4Median of Two Sorted ArraysHardsolve
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.