# 20. Modular Exponentiation for large numbers

The problem can be found at the following link: [Question Link](https://www.geeksforgeeks.org/problems/modular-exponentiation-for-large-numbers5537/1)

## My Approach

* ans is initialized to 1 to store the final result.
* x is reduced modulo M to ensure it is within the range \[0, M-1].
* While n (the exponent) is greater than 0, repeat the following steps:
* If n is odd (i.e., n % 2 == 1):
* Multiply ans by x and take modulo M.
* This ensures that any intermediate results do not exceed the limits.
* Halve n by right-shifting it by 1 bit (n >>= 1), which is equivalent to n = n / 2.
* Square x and take modulo M (x = (x \* x) % M).
* This prepares x for the next iteration.
* After the loop completes (when n becomes 0), return ans as the result.

## Time and Auxiliary Space Complexity

* **Time Complexity**: The time complexity of this approach is `O(logN)`
* **Auxiliary Space Complexity**: The auxiliary space complexity is `O(1)`.

## Code (C++)

```cpp
class Solution
{
	public:
		long long int PowMod(long long int x,long long int n,long long int M)
		{
		    long long int ans=1;
            x%=M;
            while (n>0)
            {
                if (n%2==1)
                    ans=(ans*x)%M;
                n>>=1;
                x=(x*x)%M;
            }
            return ans;
		}
};
```

## Contribution and Support

For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://github.com/getlost01/gfg-potd/discussions). We welcome your input and aim to foster a collaborative learning environment.

If you find this solution helpful, consider supporting us by giving a ⭐ star to the [getlost01/gfg-potd](https://github.com/getlost01/gfg-potd) repository.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gl01.gitbook.io/gfg-editorials/2024/05-2024-may-6/20-modular-exponentiation-for-large-numbers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
