GeeksforGeeks Medium

Nth Root of M

Nth Root of M is a GFG problem (Medium). Given two integers n and m, return the whole number x with xⁿ = m. If no whole number works, return −1.

  • The limits here are tiny (n up to 9, m up to 20), so even trying every x passes there. The common interview version allows m up to 10⁹ and n up to 30, and the method on this page is built for that.
  • m = 0 is allowed, and its root is 0.
Constraints
  • 1 <= n <= 9
  • 0 <= m <= 20
binary-searchmath
Open on GeeksforGeeks ↗
02

Intuition

As x grows, xⁿ grows too, so the candidates 0, 1, …, m are already sorted by their powers. That turns the question into a binary search over x: work out midⁿ, and if it is too small the root must be larger, if it is too big the root must be smaller. Each guess halves the range, so about log₂ m guesses are enough.

The one trap is the power itself. A value like (5 × 10⁸)³⁰ does not fit in any integer type. So stop multiplying as soon as the product passes m. Past that point the exact value does not matter, only that it is too big.

How to spot this pattern

"Find the whole number x with f(x) = target" where f only grows as x grows is binary search on the answer. Square root and cube root are the same problem with n fixed.

03

Approach

Try it first

Before reading on, check that xⁿ only grows as x grows, and decide the smallest and largest value the root could have. Then work out how to tell "midⁿ is bigger than m" without computing a number that overflows. Aim for O(n log m).

1

Search x between 0 and m

Set lo = 0 and hi = m. For x ≥ 1, xⁿ is at least x, so the root is never larger than m. Starting at 0 covers m = 0.

2

Raise mid to the nth power with a cap

Multiply p by mid up to n times, and break out as soon as p > m. Before each multiply p is at most m, so even with m = 10⁹ the product stays under 10¹⁸ and fits in a 64-bit integer.

3

Compare the power with m

If p == m, mid is the root, so return it. If p < m, every x up to mid is too small, so set lo = mid + 1. If p > m, every x from mid up is too big, so set hi = mid - 1.

4

An empty range means −1

When lo passes hi, m lies strictly between two neighbouring powers kⁿ and (k + 1)ⁿ, so no whole number has m as its nth power.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def nthRoot(self, n: int, m: int) -> int:
▶3 lo, hi = 0, m
▶4 while lo <= hi:
▶5 mid = (lo + hi) // 2
▶6 p = 1
▶7 for _ in range(n):
▶8 p *= mid
▶9 if p > m:
▶10 break
▶11 if p == m:
▶12 return mid
▶13 if p < m:
▶14 lo = mid + 1
▶15 else:
▶16 hi = mid - 1
▶17 return -1
candidates 0 to 8012345678lohipowerthe root is somewhere from 0 to 8
lo0
hi8
For any x of at least 1, x cubed is at least x, so the root cannot be bigger than 8. Starting at 0 covers m = 0. Every whole number in between is a candidate.
candidates 0 to 8012345678lohimidpower4×4×4=16>8passed 8 after 2 factors → look below 4
mid4
power16
hi3
After 2 of the 3 factors the product is already 16, more than 8, so the loop stops multiplying; the full power would only be bigger. Every number from 4 to 8 is too big, so the search moves below 4.
candidates 0 to 8012345678lohimidpower1×1×1=1<81 cubed is only 1 → look above 1
mid1
power1
lo2
1 cubed is 1, less than 8. Every number from 0 to 1 has an even smaller power, so they are all ruled out and the search moves to 2 and up.
candidates 0 to 8012345678lo, himidpower2×2×2=8=82 cubed is exactly 8 → answer 2
mid2
power8
Multiplying 2 by itself 3 times gives exactly 8, so 2 is the root and the search stops.
05

Common pitfalls

Trusting a floating-point root

✗ Wrong
x = int(m ** (1 / n))
✓ Right
binary search with exact integer powers

For n = 3, m = 1000, 1000 ** (1 / 3) is 9.999999999999998, which int turns into 9. Since 9³ is not 1000, the code returns −1 for a number whose cube root is exactly 10.

Computing the whole power before comparing

✗ Wrong
for k in range(n):
    p *= mid
✓ Right
for k in range(n):
    p *= mid
    if p > m:
        break

In the interview version with n = 30, m = 10⁹, the first guess is 5 × 10⁸, and its 30th power wraps around in a 64-bit integer. The wrapped value can look small, sending the search the wrong way. (Python's big integers survive this but get slow.)

06

Edge cases

m is 0 or 1

The root is m itself for every n. lo starts at 0, so 0 is a candidate, and 1 is found on the second guess at most.

n is 1

Every m is its own first root. The search narrows until mid = m, where the power equals m.

07

Complexity

Time
O(n log m)
Space
O(1)
The search makes about log₂ m guesses and each guess does at most n multiplications. Only a few numbers are stored.
08

Nth Root of M FAQ

How do I get the floor of the root when it is not exact?

Return hi instead of −1 after the loop. The search ends with hi on the largest x whose power is below m. With n = 2 this is Sqrt(x).

Is Newton's method faster?

It needs fewer steps, but it works with real numbers. You still have to round its result and check xⁿ = m with integers, so for a yes-or-no answer binary search is simpler to get right.