Show problems as
Classic problem #3932MediumMathBinary Search

Count K-th Roots in a Range

Real-world version: Whole Numbers Whose Power Lands in Range
Tracked in this browser only
Read Count K-th Roots in a Range on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Given l, r and k, count the whole numbers x at or above zero for which x raised to the power k is at least l and at most r.

Examples

Example 1

Input
l = 17, r = 4913, k = 3
Output
15

The cubes landing between 17 and 4913 run from 3 cubed, which is 27, up to 17 cubed, which is 4913, so fifteen bases qualify.

Example 2

Input
l = 100, r = 121, k = 2
Output
2

The squares in range are 100 and 121, from the bases 10 and 11.

Example 3

Input
l = 101, r = 120, k = 2
Output
0

No square lands between 101 and 120, since the nearest are 100 just below and 121 just above.

Constraints

  • 0 <= l <= r <= 10^9
  • 1 <= k <= 30

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def count_kth_roots(l: int, r: int, k: int) -> int:
Java
public int countKthRoots(int l, int r, int k)