Find (a^b)%m
Easy Accuracy: 38.61% Submissions: 523 Points: 2

Given three numbers a, b and m. The task is to find (a^b)%m.
Note: a is given as a String because it can be very big.

 

Example 1:

Input:
a = "3", b = 2, m = 4
Output:
1
Explanation:
(3^2)%4 = 9%4 = 1

Example 2:

Input:
a = "8", b = 2, m = 6
Output:
4
Explanation:
(8^2)%6 = 64%6 = 4 

 

Your Task:
You don't need to read input or print anything. Your task is to complete the function powerMod() which takes a string a and 2 integers b, and m as input and returns (ab % m).

 

Expected Time Complexity: O(Number of digits in a)
Expected Auxiliary Space: O(1)

 

Constraints:
1 <= |a| <= 105
0 <= b <= 109
1 <= m <=109

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.


to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Find (a^b)%m

Output Window