Find Nth root of M

Easy Binary Search Search on Answer Range Original on GeeksforGeeks

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.

00:00
Testcase

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.

ApproachTimeSpace
Linear searchO(m^(1/n) · n)O(1)
Optimal (binary search on the answer)O(n · log m)O(1)

2Optimal (binary search on the answer)

TimeO(n · log m)
SpaceO(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.

  1. lo = 1, hi = m.
  2. 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;
    }
}

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.