Please check code for Modular Exponentiation Problem


Not able to pass all test cases

hello @sahilkhan2312000131

check now->

I am not getting why is sum*=(a%c); wrong and sum=(sum*a)%c; correct??

it will overflow, we are never ensuring that sum remain inside the range of long long.
all we are doing is multilying sum(unbounded) with a which is in range [0…c-1]

just run this and see->

I am not getting what you are saying. Can you please elaborate the difference between the two?

recheck the link , by mistake i shared wrong link . . . .
that will clear ur doubt. . . . .

In the link that you have shared:
If I take sum*=(a%c); 10^9 will be multiplied 3 times in the loop and will be saved in sum
If I take sum=(sum*a)%c; 10^9 will be multiplied twice then modulo will be calculated then again 10^9 will be multiplied.
Am I correct?

yeah . . . . .
…
, . .
. .

Thank you so much for your time

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.