VOOZH about

URL: https://www.geeksforgeeks.org/dsa/implementation-of-chinese-remainder-theorem-inverse-modulo-based-implementation/

⇱ Implementation of Chinese Remainder theorem (Inverse Modulo based implementation) - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Implementation of Chinese Remainder theorem (Inverse Modulo based implementation)

Last Updated : 23 Jul, 2025

We are given two arrays num[0..k-1] and rem[0..k-1]. In num[0..k-1], every pair is coprime (gcd for every pair is 1). We need to find minimum positive number x such that: 

 x % num[0] = rem[0], 
 x % num[1] = rem[1], 
 .......................
 x % num[k-1] = rem[k-1]

Example: 

Input: num[] = {3, 4, 5}, rem[] = {2, 3, 1}
Output: 11
Explanation: 
11 is the smallest number such that:
 (1) When we divide it by 3, we get remainder 2. 
 (2) When we divide it by 4, we get remainder 3.
 (3) When we divide it by 5, we get remainder 1.

We strongly recommend to refer below post as a prerequisite for this.

Chinese Remainder Theorem | Set 1 (Introduction)
We have discussed a Naive solution to find minimum x. In this article, an efficient solution to find x is discussed.
The solution is based on below formula.

x = ( ? (rem[i]*pp[i]*inv[i]) ) % prod
 Where 0 <= i <= n-1

rem[i] is given array of remainders

prod is product of all given numbers
prod = num[0] * num[1] * ... * num[k-1]

pp[i] is product of all divided by num[i]
pp[i] = prod / num[i]

inv[i] = Modular Multiplicative Inverse of 
 pp[i] with respect to num[i]

Example: 

Let us take below example to understand the solution
 num[] = {3, 4, 5}, rem[] = {2, 3, 1}
 prod = 60 
 pp[] = {20, 15, 12}
 inv[] = {2, 3, 3} // (20*2)%3 = 1, (15*3)%4 = 1
 // (12*3)%5 = 1

 x = (rem[0]*pp[0]*inv[0] + rem[1]*pp[1]*inv[1] + 
 rem[2]*pp[2]*inv[2]) % prod
 = (2*20*2 + 3*15*3 + 1*12*3) % 60
 = (80 + 135 + 36) % 60
 = 11

Refer this for nice visual explanation of above formula.

Below is the implementation of above formula. We can use Extended Euclid based method discussed here to find inverse modulo. 

Output: 

x is 11

Time Complexity : O(N*LogN)

Auxiliary Space : O(1)

Comment
Article Tags: