Code RoomSolve two congruences
HardPrep Room Coding #1406

Solve two congruences

CodingAlgorithms & data structuresSenior–Staff~35 min

Solve a system of two congruences: find the smallest non-negative integer x such that x is congruent to r1 modulo m1 and x is congruent to r2 modulo m2. The moduli are NOT assumed coprime, so a solution exists only when (r2 - r1) is divisible by gcd(m1, m2); in that case the answer is unique modulo lcm(m1, m2). Return that smallest x, or -1 if no solution exists. Use the extended Euclidean algorithm to combine the two congruences.

Implement
crt_two(r1: int, m1: int, r2: int, m2: int) → int
Examples
in[2,3,3,5]out8
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,3,5]8not run yetsample