-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2da305f
commit 67d5e18
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |