Problem 347120 · easy · Phase 03 Linear Management & Searching

Integer Square Root by Search

binary search on the answer · math

Given a non-negative integer n, return the largest integer k such that k * k <= n.

Do it with integer arithmetic only: floating-point square roots lose precision for the large values in the tests, and math.isqrt defeats the purpose of the exercise.

Examples

Input:  n = 16
Output: 4

Input:  n = 17
Output: 4

Input:  n = 0
Output: 0

Constraints

  • 0 <= n <= 10**18
  • Required time: O(log n); counting upwards from 0 is far too slow.

Goals

  • Search a numeric range instead of a list
  • Keep the last value that satisfies a monotone condition
Starting Python…