Array ( [0] => [1] => questions [2] => Arrays [3] => Modular-inversion ) Popular Algorithms | Modular inversion | THE INQUISITIVE





Modular inversion

LEVEL:Beginner

Description

For the given two numbers print the modular inversion

Input Format

Two space separated integers

Output Format

single integer


Example 1:

Input
3 11
Output
4
Example 2:

Input
5 10
Output
Does not exist
Example 3:

Input
23 45
Output
2

oops

Login to see Discussion




Approach


i)create a method modinverse and which takes a and m as argument
ii)assign m to m1 , 0 to y and 1 to x
iii)if m equal to 1 return 0
iv)while a is greater than 1 assign a/m to q, t to a,update m to a%m,a to t, t to y and finally assign x-qy to x and t ot x
v)if x is less than 0 add m1 to x
vi)return the value of x

vii)in main method take the required input from the user and print the output

Time Complexity:
Space Complexity: O(1)


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution