Given a prime number N, the task is to find the closest smaller number than N such that modulo multiplicative inverse of a number under modulo N is equal to the number itself.
Input: N = 7
Modulo multiplicative inverse of all possible natural numbers from 1 to less than N are:
Modulo multiplicative inverse of 1 under modulo N(=7) is 1.
Modulo multiplicative inverse of 2 under modulo N(=7) is 4.
Modulo multiplicative inverse of 3 under modulo N(=7) is 5.
Modulo multiplicative inverse of 4 under modulo N(=7) is 2.
Modulo multiplicative inverse of 5 under modulo N(=7) is 3.
Modulo multiplicative inverse of 6 under modulo N(=7) is 6.
Therefore, the nearest smaller number to N(= 7) having modulo inverse equal to the number itself is 6.
Input: N = 11
Naive Approach: The simplest approach to solve this problem is to traverse all natural numbers from 1 to N and find the largest number such that modulo multiplicative inverse of the number under modulo N is equal to the number itself.
Time Complexity: O(N * log N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach the idea is based on the following observations:
The nearest smaller number to N having modulo multiplicative inverse equal to the number itself is (N – 1).
If X and Y are two numbers such that (X * Y) % N = 1 mod(N), then Y is modulo inverse of X.
Put X = N – 1 then
=>((N – 1) * Y) % N = 1 mod(N)
=>(N × Y) % N – Y % N = 1 mod(N)
=> Y = N – 1
Therefore, for X = N – 1 the value of Y is equal to X.
Therefore, to solve the problem, simply print N – 1 as the required answer.
Below is the implementation of the above approach:
Time Complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count array elements having modular inverse under given prime number P equal to itself
- Modular multiplicative inverse from 1 to n
- Modular multiplicative inverse
- Count decrements to nearest smaller element required to make all array elements equal
- Expressing a fraction as a natural number under modulo 'm'
- Chinese Remainder Theorem | Set 2 (Inverse Modulo based Implementation)
- Find the value of P and modular inverse of Q modulo 998244353
- Compute n! under modulo p
- Euler's criterion (Check if square root under modulo p exists)
- Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
- Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
- Multiply large integers under large modulo
- Equalizing array using increment under modulo 3
- Find Square Root under Modulo p | (When p is product of two primes in the form 4*i + 3)
- Multiplicative order
- Print all multiplicative primes <= N
- Count of multiplicative partitions of N
- Multiplicative Congruence method for generating Pseudo Random Numbers
- Count subarrays having sum modulo K same as the length of the subarray
- Smallest number to be added in first Array modulo M to make frequencies of both Arrays equal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : akhilsaini