Topic: Mathematics
Questions Successfully Completed: 1
1) Modular Multiplicative Inverse | Easy |
Question
Input: a = 3 m = 11 Output: 4 Explanation: Since (43) mod 11 = 1, 4 is modulo inverse of 3. One might think, 15 also as a valid output as "(153) mod 11" is also 1, but 15 is not in ring {0, 1, 2, ... 10}, so not valid.
class Solution
{
public int modInverse(int a, int m)
{
for(int i=1;i<m;i++){
if((i*a)%m==1)
return i;
}
return -1;
}
}
Thank you for reading :)