Skip to content
RED SAGSkills

Integer Square Root

A microcontroller has no floating-point unit. Given a non-negative integer n, return the largest integer r with r * r <= n.

Solve it with integer operations (for example binary search) rather than Math.sqrt.

Example 1

Input: n = 8
Output: 2
Explanation: 2 * 2 = 4 <= 8 but 3 * 3 = 9 > 8.

Example 2

Input: n = 16
Output: 4

Constraints

  • 0 <= n <= 2^31 - 1
Hint 1

The answer lies between 0 and n.

Hint 2

Binary search for the last r whose square does not exceed n.

Good practice for: Embedded Systems Engineer, Game Developer, Backend Developer, Blockchain Developer, Automation Test Engineer

Run checks your code on 2 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.