Generalized Chinese remainder theorem
You are given two equal-length lists, remainders and moduli, describing a system of congruences x ≡ remainders[i] (mod moduli[i]) for each i. The moduli are positive but NOT necessarily pairwise coprime. Return the smallest non-negative x satisfying all congruences, or -1 if the system is inconsistent. For example, x ≡ 2 (mod 3), x ≡ 3 (mod 5), x ≡ 2 (mod 7) yields 23. All values fit in 64-bit integers.
Implement
crt(remainders: list[int], moduli: list[int]) → intExamples
in
[[2,3,2],[3,5,7]]out23What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 35 min
solution.py
InputExpectedGot
[[2,3,2],[3,5,7]]23not run yetsample