Timer is Running

Modular Exponentiation for large numbers
Submissions: 64   Accuracy:


  Difficulty: Medium   Marks: 4

Implement pow(x, n) % M.
In other words, given x, n and M, find (xn
) % M.

Example 1:

Input: 3 2 4
Output: 1

Example 2:

Input: 10 9 6
Output: 4


Your Task:
You don't need to read or print anything. Your task is to complete the function PowMod() which takes the x, n and M as input parameters and returns xn % M.


Expected Time Complexity: O(log(n))
Expected Space Complexity: O(1)


1 <= x, n, M <= 100000

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Madhur Adlakha
Author: madhuradlakha

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.