Find Nth root of M | Practice | GeeksforGeeks

You are given 2 numbers (n , m); the task is to find n√m (nth root of m).
 
Example 1:
Input: n = 2, m = 9
Output: 3
Explanation: 32 = 9

Example 2:
Input: n = 3, m = 9
Output: -1
Explanation: 3rd root of 9 is not
integer.

 
Your Task:
You don't need to read or print anyhting. Your task is to complete the function NthRoot() which takes n and m as input parameter and returns the nth root of m. If the root is not integer then returns -1.
 
Expected Time Complexity: O(n* log(m))
Expected Space Complexity: O(1)
 
Constraints:
1 <= n <= 30
1 <= m <= 109
+4
yoginpahuja_033 weeks ago
        int l=1;
	    int h=m;
	    while(l<=h)
	    {
	        int mid=(l+h)/2;
	        if(pow(mid,n)==m)
	        return mid;
	        else if(pow(mid,n)>m)
	        h=mid-1;
	        else
	        l=mid+1;
	    }
	    return -1;
0
manishmawatwal1 month ago
 def NthRoot(self, n, m): # Code here ans = pow(m, 1 / n)       ans_floor = math.floor(ans)       ans_ceil = math.ceil(ans)       if math.pow(ans_floor, n) == m:           return ans_floor       elif math.pow(ans_ceil, n) == m:           return ans_ceil       else:           return -1
0
shivshivamsahu1 month ago
 int NthRoot(int n, int m){    // Code here.    if(n == 1){        return m;    }    if(m == 1){        return 1;    }        int low = 1, high = sqrt(m);    while(low <= high){        int mid = low + (high - low)/2;        long long  power = 1;        for(int i = 1; i <= n; i++){            power *= mid;            if(power > m*1LL){                break;            }        }                if(power == m){            return mid;        }                if(power > m){            high = mid-1;        }                else{            low = mid+1;        }            }    return -1;}  
+2
dheepanbalajib2 months ago
int NthRoot(int n, int m){
	    long int a=0;
	    long int ele=0;
	    while(ele<=m){
	        ele=pow(a,n);
	        if(ele==m){
	            return a;
	        }
	         a++;
	    }
	    return -1;
     }  
     #simple solution in c++;
+2
rohankundu8593 months ago
//C++ Solution
 
 int NthRoot(int n, int m){    // Code here.    int start=1;    int end=m;    while(start<=end)    {        int mid=(start+end)/2;        if(pow(mid,n)==m)        {            return mid;        }        else if(pow(mid,n)>m)        {            end=mid-1;        }        else if(pow(mid,n)<m)        {            start=mid+1;        }             }    return -1;} 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.