20(May) Modular Exponentiation for large numbers

20. Modular Exponentiation for Large Numbers

The problem can be found at the following link: Question Link

Problem Description

Given three integers x, n, and M, implement a function PowMod to compute ( (x^n) % M ).

Example 1:

Input:

x = 3, n = 2, m = 4

Output:

1

Explanation: ( 3^2 = 9 ). ( 9 % 4 = 1 ).

My Approach

  1. Base Case:

    • If ( n = 0 ), return 1 because any number raised to the power of 0 is 1.

  2. Recursive Calculation:

    • Reduce the problem by dividing ( n ) by 2, and recursively calculate the result for half the exponent.

    • If ( n ) is even, the result is the square of the half result modulo ( M ).

    • If ( n ) is odd, multiply the base ( x ) with the square of the half result modulo ( M ).

  3. Modulo Operation:

    • Ensure all intermediate results are taken modulo (M) to avoid overflow and to maintain the result within the bounds of (M).

Time and Auxiliary Space Complexity

  • Expected Time Complexity: (O(\log n)), as the problem size is halved in each recursive step.

  • Expected Auxiliary Space Complexity: (O(1)), as the space used is constant and independent of the input size.

Code

C++

Java

Python

Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


📍Visitor Count

Last updated