Skip to content

Commit

Permalink
Modulo multiplicative Inverse
Browse files Browse the repository at this point in the history
  • Loading branch information
Raushan-Kumar007 committed Jun 22, 2022
1 parent 2da305f commit 67d5e18
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions mathematic/Problems/ModuloMultiplictiveInverse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

/*Given two integers ‘a’ and ‘m’. The task is to find the smallest modular multiplicative inverse of ‘a’ under modulo ‘m’.
Example 1:
Input:
a = 3
m = 11
Output: 4
Explanation: Since (4*3) mod 11 = 1, 4
is modulo inverse of 3. One might think,
15 also as a valid output as "(15*3)
mod 11" is also 1, but 15 is not in
ring {0, 1, 2, ... 10}, so not valid.
Example 2:
Input:
a = 10
m = 17
Output: 12
Explanation: Since (12*10) mod 17 = 1,
12 is the modulo inverse of 10.
Your Task:
You don't need to read input or print anything. Your task is to complete the function function modInverse() that takes a and m as input parameters and returns modular multiplicative inverse of ‘a’ under modulo ‘m’. If the modular multiplicative inverse doesn't exist return -1.
Expected Time Complexity : O(m)
Expected Auxilliary Space : O(1)
Constraints:
1 <= a <= 104
1 <= m <= 104 */


package mathematic.Problems;

public class ModuloMultiplictiveInverse {
public int modInverse(int a, int m)
{
//Your code here
for (int x = 1; x < m; x++)
if (((a%m) * (x%m)) % m == 1)
return x;
return -1;
}
}

0 comments on commit 67d5e18

Please sign in to comment.