Find nth root of m

medium

You are given two integers n and m. Your task is to find the nth root of m — the integer x such that x^n = m.

If no such integer exists (the root is not a whole number), return -1.

In other words: find x with x * x * ... * x (n times) exactly equal to m, or report that it is impossible.

Hints

If some integer x satisfies x^n = m, how large can x possibly be? That bounds where you need to look.
As x grows, x^n only grows. So the candidates 1^n, 2^n, 3^n, … form a sorted sequence you never have to build.
Binary search the range [1, m]: compare mid^n with m — but multiply one factor at a time and stop the moment the partial product exceeds m, so nothing ever overflows.

Common doubts

Floating-point roots drift: the cube root of 27 can come out as 2.9999999999, and rounding the wrong way silently returns -1 for a perfect power. You would need to check the rounded value and its neighbors with exact integer math anyway — the integer binary search does that exactness natively.
Fully computed, yes — spectacularly. The fix is the capped power: multiply one factor at a time and return too big as soon as the running product exceeds m. The partial product then never grows past m * mid, which fits comfortably in 64 bits.
m = 1 returns 1 for every n, since 1^n = 1. n = 1 returns m itself, since m^1 = m. Both fall out naturally if your search range is [1, m].
Binary search only needs a monotonic yes/no landscape, not an array. Since x^n strictly increases with x, comparing mid^n to m tells you which half of the candidate range survives — the sorted array is implicit.

Interview follow-ups

Same binary search — but instead of returning -1, track the last mid whose power was <= m (move lo up past it) and return that best-so-far when the range empties.
The algorithm is unchanged — that is the beauty of O(n log m). Only the arithmetic care grows: keep the capped multiplication in 64-bit (or big-integer) types, and cap hi at something like 10^610^9 depending on n, since the root shrinks fast as n grows.
Yes — Newton's method iterates x ← ((n-1)·x + m / x^(n-1)) / n and converges quadratically, which is how math libraries compute roots. In an interview, integer binary search is preferred: no convergence or precision arguments needed.

Fun facts

  • Searching a range of answers instead of an array has its own name — binary search on the answer — and it powers a whole family of interview problems: Square Root, Koko Eating Bananas, Aggressive Cows, and shipping-capacity puzzles all reuse this exact skeleton.
  • Newton described his root-finding iteration in the 1660s; refined versions of it still run inside modern CPUs and math libraries every time you call a square-root function.
  • Thirty capped multiplications per probe, thirty probes for m = 10^9 — the whole search costs under a thousand multiplications, fewer than most programs spend printing a line of text.

Asked at

AmazonMicrosoftOracleWalmart
Frequently Sometimes Occasionally
Example 1
Input: n = 3, m = 8
Output: 2
2^3 = 2 × 2 × 2 = 8, so the cube root of 8 is exactly 2.
Example 2
Input: n = 3, m = 9
Output: -1
2^3 = 8 is too small and 3^3 = 27 is too big — no integer cubed gives 9.
Example 3
Input: n = 4, m = 16
Output: 2
2^4 = 16, so the 4th root of 16 is exactly 2.
Constraints

- 1 <= n <= 30 - 1 <= m <= 10^9

Solve this problem →