Count K-th Roots in a Range - Video Solutions
Count K-th Roots in a Range | LeetCode 3932 | Weekly Contest 502 | Java | Developer Coder
Count K-th Roots in a Range - Video Solution
Watch 6 video solutions for Count K-th Roots in a Range, a medium level problem involving Math, Binary Search. This walkthrough by Developer Coder has 558 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Problem 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.
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 |