InferenceAI

Enter at least two characters to search.

Start free

Integer Square Root

easyBinary Search15 min10 XP

Return ⌊x⌋\lfloor \sqrt{x} \rfloor for a non-negative integer xx, without using a built-in square root.

Requirements

  • Binary search on the answer: O(log⁡x)O(\log x).

Examples

EXAMPLE WALKTHROUGHperfect square

A guided view of the published example, not a trace of your code.

INPUT[4]
TASKinteger_square_root()
EXPECTED OUTPUT2

Start with the input values shown in this example.

Example 1 of 3

Input: x = 4

Output: 2

Explanation: perfect square

Input: x = 8

Output: 2

Explanation: rounds down

Input: x = 0

Output: 0

Explanation: zero

Hint 1

Find the largest m with m * m <= x.

Topics

DSABinary SearchPython