↓Skip to main content
  1. LeetCode/

LeetCode 3932: Count K-th Roots in a Range

·3 mins· ·
LeetCode Medium Binary-Search Math
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-05-17

  • Submissions: WA ×4 → AC (05-17) → WA → TLE → AC → AC (rewritten on 05-18)

Approach
#

The perfect k-th powers in [l, r] correspond to integers x with l <= x^k <= r.

Let f(n) be the largest x with x^k <= n. Then:

answer = f(r) - f(l - 1)

Note that 0 = 0^k is a perfect k-th power too, so x starts at 0, and f(-1) = -1.

My Attempts
#

Attempt 1: comparing floats for equality
#

if i ** (1 / k) == int(i ** (1 / k)):

Floating-point roots are inexact: in Python, 64 ** (1/3) is 3.9999999999999996, so comparing with int(...) says 64 is not a perfect cube. It also checked every value in [l, r] one by one, which is far too slow for 10^9.

Integer binary search, but missing 0
#

if n == 0:
    return 0
low = 1

The search started at 1 and returned 0 for n == 0, which excludes x = 0. With l = 0, the perfect power 0 was missed. Starting the search at 0 and returning -1 for n < 0 passed.

The next day: enumerating bases
#

I rewrote it to enumerate x from 0 to r^(1/k) + 1 and check l <= x^k <= r. With k = 1, x runs up to 10^9 and times out, so a k == 1 special case was needed.

The last version used floats only to estimate the range of x and corrected both ends with integers, which also passed.

Solution
#

class Solution:
    def countKthRoots(self, l: int, r: int, k: int) -> int:
        def floor_root(n: int) -> int:            # largest x with x ** k <= n
            lo, hi = 0, n + 1
            while lo < hi:
                mid = (lo + hi) // 2
                if mid ** k <= n:
                    lo = mid + 1
                else:
                    hi = mid
            return lo - 1                          # -1 when n < 0

        return floor_root(r) - floor_root(l - 1)

Everything is integer arithmetic, and k = 1 needs no special case.

Complexity
#

Time O(k log r): log r iterations, each computing mid ** k. Space O(1).

Mistakes to Avoid
#

  • Taking a floating-point root and comparing for equality.
  • Missing 0: when l = 0, 0 itself is a perfect k-th power.
  • Enumerating bases without considering k = 1: there would be up to 10^9 bases.

Interview Explanation
#

The count of perfect k-th powers in the range is f(r) - f(l - 1), where f(n) is the largest integer x with x^k <= n. Floating-point roots are inexact, so I compute f with integer binary search starting from 0, which counts 0 and naturally gives f(-1) = -1. Time O(k log r).

Related

LeetCode 633: Sum of Square Numbers
·2 mins
LeetCode Medium Two-Pointers Math Binary-Search
LeetCode 633 note: converge a from 0 and b from isqrt(c); shrink b when the sum is too big and grow a when it is too small; watch for 0, a == b, and floating-point square roots
LeetCode 3824: Minimum K to Reduce Array Within Limit
·2 mins
LeetCode Medium Binary-Search Math
LeetCode 3824 note: binary search on k with a monotonic check; the upper bound must reach n because there are at least n operations
LeetCode 3918: Sum of Primes Between Number and Its Reverse
·2 mins
LeetCode Medium Math Number-Theory
LeetCode 3918 note: reverse n, take the range [min, max], and sum primes by trial division; test divisibility with %, never by comparing // with /
LeetCode 3899: Angles of a Triangle
·2 mins
LeetCode Medium Math Geometry
LeetCode 3899 note: check the triangle inequality first, compute the three angles with the law of cosines, and remember to sort them
LeetCode 2563: Count the Number of Fair Pairs
·3 mins
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 2563 note: split “sum in [lower, upper]” into “sum <= upper” minus “sum <= lower - 1”, and count each with converging pointers over the sorted array in O(n)
LeetCode 1498: Number of Subsequences That Satisfy the Given Sum Condition
·2 mins
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 1498 note: only the minimum and maximum matter, so order does not and we can sort; fixing nums[left] as the minimum, the elements between are free, giving 2^(right - left) choices