Modular exponential 628


i am getting wrong answer for one test case please help where i m wrong

Hello @neeleshr628,

This is because you are not considering all the cases:

  1. if b is odd:
    a^b=a*(a^(b-1))

  2. if b is even:
    a^b=(a^(b/2))*(a^(b/2))

Thus,
a^b%c=(a%c*(a^(b-1))%c)%c , for b odd
a^b=((a^(b/2))%c*(a^(b/2))%c)%c , for b even

I have given enough hint to solve this problem.
Now, it’s your task to code it and pass all the testcases successfully.
Use loops, or recursion, or which so ever approach you can think of.

Hope, this would help.
Give a like, if you are satisfied.

but it is giving the right output for odd powers too.

@neeleshr628,

Your code is giving wrong output for inputs like:
2 1 3
when b=1
Expected Output:
2
Your Output:
24195064
Why?
what if i say that these are two different outputs.
Yes, 2 is for b==1 and 4195064 is a junk value for ans, that your code prints at last.

I have modified your code.
Now, it is running correct:

Hope, this would help.
Give a like, if you are satisfied.

where have you changed can you please tell so that in future i wont make that mistake again

I have mentioned the comment, you can check.