[Multiplicative Inverse] failing few test cases

Problem : https://hack.codingblocks.com/contests/c/473/695
Code : https://ide.codingblocks.com/#/s/14822

Implementing extended Euclid’s method to calculate multiplicative modulo inverse but 3 / 5 test cases are failing. Any corner cases or errors?

You’re not handling the case when the result will be negative. You need to do ans = (ans%m + m)%m or ans = ans < 0 ? ans+1000000007 : ans and it’ll pass.

1 Like

:sweat_smile: Fixed it, AC :+1:

1 Like