Find Nth root of a number
Pattern: Binary search on the answer
Binary search the root between 1 and M. Compare mid to the power N with M, stopping the multiplication as soon as it passes M so it never overflows. Return -1 if no whole number fits exactly.
The problem
Given two whole numbers N and M, return the whole number X with X to the power N equal to M. If no whole
number works, return -1. For example, N = 3, M = 27 gives 3, N = 4, M = 81 gives 3, and N = 4, M = 69 gives -1.
The function name NthRoot matches the takeUforward practice page. Use the link above for the exact statement and
limits.
Think first
- As
Xgrows, how doesX^Nchange? - What range must the root lie in?
14³is fine, but what about14³⁰? How do you compare it withMsafely?
Hints
Brute force, and why it fails
Try X = 1, 2, 3, … until X^N reaches M: up to about M^(1/N) tries, each with N multiplications. Floating point
M^(1/N) is fast but can round wrong. Binary search is exact and O(N log M).
The key insight
A power grows with its base, so the "is it too big" check is monotonic. Stopping the multiplication early turns a possibly enormous number into a safe comparison.
Dry run
N = 3 and M = 27. The root, if it is a whole number, lies between 1 and 27.
The plan
lo = 1, hi = M
while lo <= hi:
mid = lo + (hi - lo) / 2
p = 1
repeat N times: p = p * mid; if p > M: stop
if p == M: return mid
if p < M: lo = mid + 1
else: hi = mid - 1
return -1
Complexity
- Time
O(N log M)- Space
O(1)
About log₂ M guesses, each with at most N multiplications.
Edge cases
N = 1: the answer isM.M = 1: the answer is 1 for anyN.N = 30, M = 10⁹: only 1 is small enough, and1^30 ≠ 10⁹, so -1.
I've built my logic, show the code
Java
class Solution {
public int NthRoot(int N, int M) {
int lo = 1, hi = M;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
int c = compare(mid, N, M); // is mid^N below, equal to or above M?
if (c == 0) return mid;
if (c < 0) lo = mid + 1;
else hi = mid - 1;
}
return -1; // no whole number works
}
private int compare(long base, int n, int m) {
long p = 1;
for (int i = 0; i < n; i++) {
p *= base;
if (p > m) return 1; // stop early: the power is already too big
}
return p == m ? 0 : -1;
}
}C++
class Solution {
public:
int NthRoot(int N, int M) {
int lo = 1, hi = M;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
int c = compare(mid, N, M); // is mid^N below, equal to or above M?
if (c == 0) return mid;
if (c < 0) lo = mid + 1;
else hi = mid - 1;
}
return -1; // no whole number works
}
int compare(long long base, int n, int m) {
long long p = 1;
for (int i = 0; i < n; i++) {
p *= base;
if (p > m) return 1; // stop early: the power is already too big
}
return p == m ? 0 : -1;
}
};Python
class Solution:
def NthRoot(self, N: int, M: int) -> int:
lo, hi = 1, M
while lo <= hi:
mid = (lo + hi) // 2
c = self.compare(mid, N, M) # is mid^N below, equal to or above M?
if c == 0:
return mid
if c < 0:
lo = mid + 1
else:
hi = mid - 1
return -1 # no whole number works
def compare(self, base: int, n: int, m: int) -> int:
p = 1
for _ in range(n):
p *= base
if p > m:
return 1 # stop early: the power is already too big
return 0 if p == m else -1Common mistakes
- Using
Math.powor**with floats and rounding, which can be off by one for large values. - Multiplying all
Ntimes before comparing, which overflows. - Returning the floor of the root instead of -1 when it is not exact.