Find Nth root of M
Find Nth root of M is a easy binary search problem solved with the search on answer range pattern.
The best approach, optimal (binary search on the answer), runs in O(n · log m) time and O(1) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from linear search up.
Problem
Given n and m, return the integer x with x^n == m, or -1 if no such integer exists.
Examples
Example 1
- Input
n = 3, m = 27- Output
3
Example 2
- Input
n = 4, m = 69- Output
-1- Why
- 69 is not a perfect 4th power.
Constraints
1 <= n <= 30,1 <= m <= 10^9- Return the integer root, or -1 if it is not an integer.
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (2 examples, or your own). Submit also runs 14 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Linear search | O(m^(1/n) · n) | O(1) |
| Optimal (binary search on the answer) | O(n · log m) | O(1) |
1Linear search
O(m^(1/n) · n)O(1)Try x = 1, 2, 3, ... until x^n reaches or passes m.
- For x from 1: compute x^n (capped); equal → x, greater → -1.
class Solution {
public int nthRoot(int n, int m) {
for (long x = 1; x <= m; x++) {
long p = power(x, n, m);
if (p == m) return (int) x;
if (p > m) return -1;
}
return -1;
}
private long power(long x, int n, long cap) {
long p = 1;
for (int i = 0; i < n; i++) {
p *= x;
if (p > cap) return cap + 1;
}
return p;
}
}class Solution:
def nthRoot(self, n: int, m: int) -> int:
for x in range(1, m + 1):
p = self.power(x, n, m)
if p == m:
return x
if p > m:
return -1
return -1
def power(self, x: int, n: int, cap: int) -> int:
p = 1
for _ in range(n):
p *= x
if p > cap:
return cap + 1
return pclass Solution {
public:
int nthRoot(int n, int m) {
for (long long x = 1; x <= m; x++) {
long long p = power(x, n, m);
if (p == m) return (int)x;
if (p > m) return -1;
}
return -1;
}
private:
long long power(long long x, int n, long long cap) {
long long p = 1;
for (int i = 0; i < n; i++) {
p *= x;
if (p > cap) return cap + 1;
}
return p;
}
};function nthRoot(n, m) {
for (let x = 1; x <= m; x++) {
const p = power(x, n, m);
if (p === m) return x;
if (p > m) return -1;
}
return -1;
}
function power(x, n, cap) {
let p = 1;
for (let i = 0; i < n; i++) {
p *= x;
if (p > cap) return cap + 1;
}
return p;
}2Optimal (binary search on the answer)
O(n · log m)O(1)Search x in [1, m]. Compute mid^n but stop early as soon as the product exceeds m, which also prevents overflow. Equal means found; too big means search left; too small means search right.
- lo = 1, hi = m.
- p = capped mid^n; p == m → mid; p < m → lo = mid + 1; else hi = mid - 1.
class Solution {
public int nthRoot(int n, int m) {
long lo = 1, hi = m;
while (lo <= hi) {
long mid = (lo + hi) / 2;
long p = power(mid, n, m);
if (p == m) return (int) mid;
if (p < m) lo = mid + 1; else hi = mid - 1;
}
return -1;
}
private long power(long x, int n, long cap) {
long p = 1;
for (int i = 0; i < n; i++) {
p *= x;
if (p > cap) return cap + 1;
}
return p;
}
}class Solution:
def nthRoot(self, n: int, m: int) -> int:
lo, hi = 1, m
while lo <= hi:
mid = (lo + hi) // 2
p = self.power(mid, n, m)
if p == m:
return mid
if p < m:
lo = mid + 1
else:
hi = mid - 1
return -1
def power(self, x: int, n: int, cap: int) -> int:
p = 1
for _ in range(n):
p *= x
if p > cap:
return cap + 1
return pclass Solution {
public:
int nthRoot(int n, int m) {
long long lo = 1, hi = m;
while (lo <= hi) {
long long mid = (lo + hi) / 2;
long long p = power(mid, n, m);
if (p == m) return (int)mid;
if (p < m) lo = mid + 1; else hi = mid - 1;
}
return -1;
}
private:
long long power(long long x, int n, long long cap) {
long long p = 1;
for (int i = 0; i < n; i++) {
p *= x;
if (p > cap) return cap + 1;
}
return p;
}
};function nthRoot(n, m) {
let lo = 1, hi = m;
while (lo <= hi) {
const mid = Math.floor((lo + hi) / 2);
const p = power(mid, n, m);
if (p === m) return mid;
if (p < m) lo = mid + 1; else hi = mid - 1;
}
return -1;
}
function power(x, n, cap) {
let p = 1;
for (let i = 0; i < n; i++) {
p *= x;
if (p > cap) return cap + 1;
}
return p;
}Edge cases to test
- n = 1 (answer is m)
- mid^n overflows long: stop multiplying once it exceeds m
Hints
Hint 1
The answer lies in [1, m], and mid^n grows with mid, so you can binary search on the answer.
FAQ
What is the best time complexity for Find Nth root of M?
Optimal (binary search on the answer) runs in O(n · log m) time and O(1) extra space.
Which pattern does Find Nth root of M use?
It is a binary search problem that uses the search on answer range pattern. Other problems with the same pattern: Pow(x, n) (Binary Exponentiation), Sqrt(x), Find position of an element in a sorted array of infinite numbers (Galloping Search).
Is there a brute force solution for Find Nth root of M?
Yes. Linear search takes O(m^(1/n) · n) time and O(1) space. Try x = 1, 2, 3, ...
Which edge cases should I test for Find Nth root of M?
n = 1 (answer is m); mid^n overflows long: stop multiplying once it exceeds m.