Count K-th Roots in a Range

You are given three integers l, r, and k.

An integer y is said to be a perfect k^th power if there exists an integer x such that y = x^k.

Return the number of integers y in the range [l, r] (inclusive) that are perfect k^th powers.

Example 1
Inputl = 1, r = 9, k = 3
Output2
The perfect cubes in the range [1, 9] are 1 = 1^3 and 8 = 2^3, so the answer is 2.
Example 2
Inputl = 8, r = 30, k = 2
Output3
The perfect squares in the range [8, 30] are 9 = 3^2, 16 = 4^2, and 25 = 5^2, so the answer is 3.

Constraints

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

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate