459. Check if N-th Root of M Exists

EasyBinary SearchMathBinary Search

Given two integers n and m, return true if there exists an integer x such that x^n == m. Otherwise return false. Do not use built-in power or root functions. Solve in O(log m).

Input: Integers n (the root degree) and m (the value).

Output: true if integer nth root of m exists, false otherwise.

Examples

Example 1
Input: 3, 27
Output: true
Explanation: 3^3=27.
Example 2
Input: 2, 14
Output: false
Explanation: No integer r with r^2=14.
Example 3
Input: 4, 16
Output: true
Explanation: 2^4=16.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →