Code RoomGeneralized Chinese remainder theorem
HardPrep Room Coding #760

Generalized Chinese remainder theorem

CodingAlgorithms & data structuresSenior–Staff~35 min

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]) → int
Examples
in[[2,3,2],[3,5,7]]out23
What 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
InputExpectedGot
[[2,3,2],[3,5,7]]23not run yetsample