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
0
rohankundu8591 day 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;} 
+1
mridulbhaskarabc1 month ago
#Contributed By: Mridul Bhaskar
import math
class Solution:
	def NthRoot(self, n, m):
	    
	    
		# Code here
		for i in range(m+1):
		    data_ = i**n
		    if(data_ == m):
		        return i
		    elif(data_>m):
		        return -1
		    else:
		        continue
 
0
skasifiqbal7862 months ago
Simple C++ Code-

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;
     }  
0
rahulrangi182 months ago
int NthRoot(int n, int m)
	{
	    double e=2.718281828459045;
	    double A=log(m);
	    double B=(double)A/(double)n;
	    double y=pow(e,B);
	    int ans=round(y);
	    float d=(float)ans-(float)y;
	    //cout << setprecision(20) << y<<endl;
	    //cout << setprecision(20)<<d<<endl;
	    if(d==0.0)
	      return ans;
	    else
	      return -1;
	}
+3
niraj9504052 months ago
class Solution{
	public:
	int NthRoot(int n, int m)
	{
	    long long low=1,high=m;
	    while(high>=low){
	        long long mid=(low+high)/2;
	        if(pow(mid,n)==m){
	            return mid;
	        }
	        else if(pow(mid,n)>m){
	            high=mid-1;
	        }
	        else{
	            low=mid+1;
	        }
	    }
	    return -1;
	}  
};
0
shadyboy2 months ago
	int NthRoot(int n, int m)
	{
	    long long lo = 1, hi = m;
	    
	    while(hi>=lo)
	    {
	        long long val = 1;
	        long long mi = (lo+hi)/2;
	        
	        for(int i = 1; i<= n ;i++){
	            val *= mi;
	            if(val>m)break;
	        }
	        
	        if(val==m){
	            return mi;
	        }else if(val>m){
	            hi = mi-1;
	        }else{
	            lo = mi+1;
	        }
	    }
	    return -1;
	}  
0
prateek_jakhar2 months ago
Easy to understand JAVA code
public int NthRoot(int n, int m)
    {
        // code here
        if(m==1) return 1; //1 to the power n will always be 1
        if(n==1) return m; //m to the power 1 will always be m
        for(int i = 2; i<=m; i++){ // checking for every digit
            long ans = 1;
            for(int j = 0; j<n; j++){
                ans*=i;
            }
            //no need to loop if getting larger values
            if(ans>m) break; 
            
            if(ans==m){
                return i;
            }
        }
        return -1;
    }
+1
neeramrutia2 months ago
 int i=1;       while(Math.pow(i,n)!=m)       {           i++;           if(Math.pow(i,n)>m)           return -1;       }       return i;
0
mahadevk3 months ago
binary search over reduced end by power of 2.
class Solution{
	public:
	long long fastExponentiation(long long n, long long m, long long k){
	    if(m==1)return n;
	    if(m&1)return n*fastExponentiation(n*n, m/2, k);
	    else return fastExponentiation(n*n, m/2, k);
	}
	int NthRoot(int n, int m)
	{
	    if(n==1)return m;
	    int start = 0;
	    int end = m;
	    int k = n;
	    
	    while(k>1){
	        k/=2;
	        end = sqrt(end);
	    }
	    
	    
	    while(start<=end){
	        int mid = (start+end)/2;
	        if(fastExponentiation(mid, n, m) == m){
	            return mid;
	        }else if(
	            fastExponentiation(mid, n, m)<m){
	            start = mid+1;
	        }else{
	            end = mid-1;
	        }
	    }
	    
	    return -1;
	    
	}  
};

0
siddharthabd3 months ago
long nthRoot = Math.round(Math.pow(m, (double)1/n));       if (Math.pow(nthRoot, n) == m) {           return (int) nthRoot;       } else {           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.