Exponentiation (Set 2) | Practice | GeeksforGeeks

For two given positive numbers a and b. Find ab. Output your number modulus 109+7.
Example 1:
Input: a = 1, b = 1
Output: 1
Explanation: 11 % (109+7) = 1

â€‹Example 2:
Input: a = 2, b = 5
Output: 32
Explanation: 25 % (109+7) = 32
Your Task:  
You don't need to read input or print anything. Your task is to complete the function power() which takes a, b as inputs, and returns the answer.

Expected Time Complexity: O(log b)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ a ≤ 105
1 ≤ b ≤ 1016
0
abhishekkaswan6 days ago
C++ iterative and recursive both approaches 
class Solution{   
public:
    long long int power(long long int a, long long int b) { 
        //recursve solution with O(logn) time and O(logn) space 
        // because i am saving the value of pow(a,b/2) instead of using it each ime in return statement
    
        // if(b==0)
        //     return 1;
        // long long temp=power(a,b/2);
        // // temp=temp%1000000007;
        // if(b%2==0)
        //     return (temp%1000000007*temp%1000000007)%1000000007;
        // else
        //     return (a*temp%1000000007*temp%1000000007)%1000000007;
            
        
        //now iterative solution is here, now it doesnt use stack call so space is O(1) and time is O(logn)
        //both solution is working fine
        
        long long int res=1;
        while(b>0)
        {
            if(b&1)//if b is odd
            {
                res=(res*a)%1000000007;
            }
            a=(a*a)%1000000007;
            //do the operation b=b/2;
            b=b>>1;
                
                
        }
        return res;
    }
};
0
sweta11103 months ago
class Solution{   public:   long long int MOD= 1000000007;   long long int power(int a, long long int b) {              if(b < 1){           return 1;       }             long long int halfPower= power(a,b/2);       long long int res  = 1;      if(b % 2 == 0){          res *= (halfPower) % MOD * (halfPower) % MOD;      }else{          res *= (halfPower) % MOD * (halfPower) % MOD * a;      }         return res % MOD;   }};
0
Lubna Noor Ahmad8 months ago
Lubna Noor Ahmad
class Solution{    static long power(long a, long b)    {        long res=1;        while(b>0)        {            if((b&1)!=0) //even            {               res=(res*a % 1000000007) % (1000000007);             }            a=(a % 1000000007*a % 1000000007) % (1000000007);            b=b>>1;        }        return res;
        //complete the function here    }}
0
Arham kalam8 months ago
Arham kalam
class Solution{   public:    long long int power(long long int a, long long int b) {         //complete the function here        long long int res=1;        while(b>0)        {        if(b%2==0)        {            a=(a*a)%1000000007;            b=b/2;        }        else        {            res=(res*a)%1000000007;            b=b-1;        }        }        return res;    }};
0
Himanshu Yadav11 months ago
Himanshu Yadav
long long int power(long long int a, long long int b) {
        long long int res=1;
        while(b>0)        {            if(b&1)            {                res=(res*a)% (1000000007);            }
            a = (a*a)% (1000000007);            b=b>>1;     //right shift        }
        return res;    }
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.