Count K-th Roots in a Range - Solution & Explanation
Practice this problemProblem Statement
You are given three integers l, r, and k.
An integer y is said to be a perfect kth power if there exists an integer x such that y = xk.
Return the number of integers y in the range [l, r] (inclusive) that are perfect kth powers.
Example 1:
Input: l = 1, r = 9, k = 3
Output: 2
Explanation:
The perfect cubes in the range[1, 9] are:
1 = 138 = 23
Example 2:
Input: l = 8, r = 30, k = 2
Output: 3
Explanation:
The perfect squares in the range[8, 30] are:
9 = 3216 = 4225 = 52
Constraints:
0 <= l <= r <= 1091 <= k <= 30
Approach Overview
Problem Overview: Given a range [L, R] and an integer k, count how many integers x exist such that x^k lies within the range. In other words, how many perfect k-th powers fall between L and R.
Approach 1: Brute Force Enumeration (O((R-L) * log k) time, O(1) space)
Iterate through every number in the range [L, R] and check whether it is a perfect k-th power. For each number n, compute its k-th root using floating-point math or repeated multiplication and verify if the root raised back to k equals n. This works but quickly becomes impractical when the range is large. The algorithm spends most of its time scanning numbers that cannot possibly be perfect powers.
Approach 2: Mathematical Root Boundaries (O(1) time, O(1) space)
A better observation: instead of checking numbers in the range, count the integers whose k-th power lands inside the range. If x^k must satisfy L ≤ x^k ≤ R, then x must satisfy L^(1/k) ≤ x ≤ R^(1/k). Compute low = ceil(L^(1/k)) and high = floor(R^(1/k)). The number of valid integers is max(0, high - low + 1). This converts a potentially massive iteration into a constant-time math calculation.
Approach 3: Binary Search for Integer k-th Roots (O(log R) time, O(1) space)
Floating point roots may introduce precision issues for large numbers. A safer method is to compute integer k-th roots using binary search. Search for the largest integer whose k-th power is ≤ R, and the largest integer whose k-th power is < L. Subtract the two counts to get the number of valid bases. This approach guarantees correctness even when L and R approach 64‑bit limits.
These strategies rely on recognizing that the problem is fundamentally mathematical rather than iterative. Understanding exponent growth drastically shrinks the search space.
Recommended for interviews: The mathematical boundary approach combined with careful integer root handling is the expected solution. Mention the brute force method first to demonstrate baseline reasoning, then transition to the optimized formula. Interviewers often expect candidates to recognize the x^k inequality transformation and optionally implement a safe root calculation using math or binary search.
Solution
First, we check if k equals 1. If it does, the count of perfect 1st powers in the range is the count of integers in the range, which is r - l + 1.
Otherwise, we enumerate integers x, compute y = x^k. If y exceeds r, we stop enumeration. If y is within the range [l, r], we increment the answer by 1.
The time complexity is O(r^{1/k} cdot k), and the space complexity is O(1).
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Enumeration | O((R-L) * log k) | O(1) | Small ranges or initial reasoning during interviews |
| Mathematical Root Bounds | O(1) | O(1) | General case when floating-point precision is acceptable |
| Binary Search k-th Root | O(log R) | O(1) | Large integers or when exact integer arithmetic is required |
Video Solution
Count K-th Roots in a Range | LeetCode 3932 | Weekly Contest 502 | Java | Developer Coder • Developer Coder • 558 views views
Watch 5 more video solutions →Frequently Asked Questions
Is Count K-th Roots in a Range easy or hard?
Count K-th Roots in a Range Python/Java solution
How to solve Count K-th Roots in a Range in O(1)?
What is the best approach for Count K-th Roots in a Range?
Is Count K-th Roots in a Range asked at Google/Amazon/Meta?
What data structure is used in Count K-th Roots in a Range?
What is the time complexity of Count K-th Roots in a Range?
Ready to solve this problem?
Practice Count K-th Roots in a Range with our built-in code editor and test cases.
Practice on FleetCodeTable of Contents
Practice this problem
Open in Editor