Modular Multiplicative Inverse | Practice | GeeksforGeeks

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
0
crawler2 weeks ago
Total Time Taken: 0.01/2.2
class Solution{
    public:
    //Complete this function
    int __gcd(int a, int b){
        return (b == 0 ? a : __gcd(b, a%b));
    }
    int modInverse(int a, int m)
    {
        if(__gcd(a, m) > 1)
            return -1;
        for(int i = 1; i <= m; i++){
            if((a*i)%m == 1)
                return i;
        }
        return -1;
    }
};
0
mail2rajab011 month ago
// Simple Java Solution
Total Time Taken: 0.2/1.2
 
class Solution{    public int modInverse(int a, int m)   {     //Your code here     int ans=-1;     for(int i=1;i<m;i++){         if((a*i)%m==1){             ans=i;             break;         }     }     return ans;   }
}
 
0
lawbindpandey01w1 month ago
This passed all the test cases and time complexity is O(log(max(a,m))).. you need to use Extended Euclidean algorithm .. one can also use Fermat's  little theorem but only when  m is always prime number.
 
 
class Solution{   public:   //Complete this function   int x,d,y;      void fun(int a,int m){              if(m == 0){           x = 1;           d = a;           y = 0;       }              else{           fun(m, a%m);           int temp = x;           x = y;           y = temp - y * (a / m);       }          }      int modInverse(int a, int m)   {       //Your code here       fun(a,m);       x = (x%m + m)%m;       if((a*x)%m == 1)           return (x%m + m)%m;                  return -1;          }};
+1
deekshakulshreshtha112 months ago

class Solution{
    public:
    //Complete this function
    int modInverse(int a, int m)
    {
        for(int i = 0; i<m; i++){
            if(((i*a) % m)==1){
                return i;
            }
        }
        return -1; 
       
    }
};
When I'm attempting the code, I got an error that expected ) before mod after searching for a while I got the reason of that error that mod is not working in cpp. instead that % symbol is used in cpp.
 
My accuracy score is 25% is someone getting higher then kindly ping me. 
0
nikhilbarot16022 months ago
C++ Solution:
 
All test cases passed.
Time-Complexity: O(m) (0.0/2.2)
int modInverse(int a, int m)
    {
        for(int i = 1; i <= m; i++){
            if((i * a) % m == 1)
                return i;
        }
        return -1;
    }
0
saurabhsharma295203 months ago
int gcd(int a,int b){       if (a == 0) return b;       return gcd(b % a, a);   }   int modInverse(int a, int m) {       if((gcd(a,m)) != 1 || m == 1) return -1;        int res = 999999, t = 0;       for(int i = 1;i<m;i++){           if(res>(a*i)%m){               res = (a*i)%m;               t = i;           }       }       return t;   }
0
bhawanipareek20193 months ago
class Solution{   public:   //Complete this function   int modInverse(int a, int m)   {       for(int x=1;x<m;x++){           if(((a%m) * (x%m)) % m == 1){           return x;}                  }         return -1;            }};
0
lavanyadg4444 months ago
attempt 1
class Solution{   public:   //Complete this function   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;   }};
-1
nagesh_nagshakti6 months ago
    int modInverse(int a, int m)
    {
        //Your code here
        for(int i = 1;i<m;i++){
            if(a*i%m==1)
                return i;
        }
        return -1;
    }
-1
jimmypi6546 months ago
class Solution:    
    ##Complete this function
    def modInverse(self,a,m):
        ##Your code here
        for t in range(1, m):
            if self.isInt(a, m, t):
                return t
            
        return -1
        
        
    def isInt(self, a, m, t):
        return a * t % m == 1
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.