All problems
0937MediumMathBinary Search

Whole Numbers Whose Power Lands in Range

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3932Count K-th Roots in a Range

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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