Power Of Numbers | Practice | GeeksforGeeks

Given a number and its reverse. Find that number raised to the power of its own reverse.
Note: As answers can be very large, print the result modulo 109 + 7.
Example 1:
Input:
N = 2
Output: 4
Explanation: The reverse of 2 is 2
and after raising power of 2 by 2 
we get 4 which gives remainder as 
4 by dividing 1000000007.
Example 2:
Input:
N = 12
Output: 864354781
Explanation: The reverse of 12 is 21
and 1221 , when divided by 1000000007 
gives remainder as 864354781.
Your Task:
You don't need to read input or print anything. You just need to complete the function pow() that takes two parameters N and R denoting the input number and its reverse and returns power of (N to R)mod(109 + 7).
Expected Time Complexity: O(LogN).
Expected Auxiliary Space: O(LogN).
Constraints:
1 <= N <= 109
 
0
aashishkumar09111 week ago
whats the error in this code can anyone please help
 
long long power(int N,int R)   {      //Your code here      const int m=1e+7;      long long ans=1;      while(R>0){          if(R&1)              ans=(ans*N)%m;          N=(N*N)%m;          R>>=1;      }      return ans;   }
0
ugamthakkar2 weeks ago
class Solution{   public:   const long long m = 1e9+7;   long long count(int n, int r){       long res=1;       while(r>0){           if(r&1){               res = (res* n%m)%m;                          }           n = (n%m * n%m)%m;           r = r>>1;       }       return res;   }      //You need to complete this fucntion      long long power(int N,int R)   {      //Your code here       return count(N,R);   }
};
0
arjunnigam17132 weeks ago
class Solution:
    #Complete this function
    def power(self,N,R):
        #Your code here
        
        if R==0:
            return 1
        
        temp = self.power(N,R//2)
        if R%2==0:
            return (temp*temp)%(1000000007)
        else:
            return (N*temp*temp)%(1000000007)
-6
sharankushal03 weeks ago
C++ all test cases pass🎉
long long power(long long int N,int R)
{
static long long int m=1000000007;
if(R==0)
	return 1;
if(R==1)
	return N;
if(R%2==0)
	return power((N*N)%m, R/2)%m;
else
	return (N%m)*power((N*N)%m, (R-1)/2)%m;
}
 
-1
moaslam8263 weeks ago
c++ soln
const long long m=1e9+7;
    long long fun(int n,int r){
        if(r==1){
            return n;
        }
        if(r==0){
            return 1;
        }
        long long ans=fun(n,r/2);
        if(r&1){
            return n*1LL*ans%m *ans%m;
        }
        return ans%m*1LL*ans%m;
    }
    long long power(int N,int R)
    {
 
      
       return fun(N,R);
       
     
    }
0
akkeshri140420011 month ago
long long power(int N,int R)
   {
      //Your code here
      if(R==0){
          return 1;
      }
      if(R==1){
          return N;
      }
      long long know=power(N,R/2)%mod;
      long long ans=1;
      if(R%2){
          ans=(ans*N)%mod;
      }
      return ans*((know*know)%mod)%mod;
       
   }
0
velspace011 month ago
Java Solution.Works for all test Cases
static long m=1000000007;    
    long power(long N,long R)
    {
        long ans;
        if(R==0){
            return 1;
        }
        if(R==1){
            return N;
        }
        if(R%2==0){
            ans=power((N*N)%m,R/2);
        }else{
            ans=(N%m)*power((N*N)%m,R/2);
        }
        return ans%m;
        
    }
+1
anukoolsrivastava1 month ago
Python Solution. Works for all test cases.
    
      def power(self,N,R):
        self.N=N
        if R==0:
            return 1
        ans=self.power(self.N,R//2)
        if R%2==0:
            return (ans*ans)%(1000000007)
        else:
            return self.N*ans*ans%(1000000007)
0
salonitayal1 month ago
Can anybody tell why this is wrong?
 
long long power(int N,int R)
    {
       if(R==0)return 1;
       if(R&1)
       {
           return (N%1000000007)*(power(N, R/2)%1000000007*power(N, R/2)%1000000007);
       }
       else 
       {
           return ((power(N, R/2)%1000000007)*(power(N, R/2)%1000000007))%1000000007;
       }
    }

+1
joker381 month ago
whats wrong with this code?
class Solution{   private:   long long powerFunction(int a,int b)   {       if(b==0)       {           return 1;       }       if(b==1)       {           return a;       }       long long ans=powerFunction(a,b/2);       if(b%2==0)       {           return ans*ans;       }       else       {           return a*ans*ans;       }   }   public:   //You need to complete this fucntion      long long power(int N,int R)   {      //Your code here      long long int m= 1000000007;      long long ans= powerFunction(N,R)%m;      return ans;          }
};
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.