Code RoomModular inverse extended euclidean
MediumPrep Room Coding #1396

Modular inverse extended euclidean

CodingAlgorithms & data structuresMid–Senior~25 min

Compute the modular multiplicative inverse of a modulo m, i.e. the integer x in [0, m) with (a * x) mod m == 1, or return -1 if no inverse exists (when gcd(a, m) != 1). Here m may NOT be prime, so Fermat's little theorem does not apply; use the extended Euclidean algorithm. By convention return 0 when m == 1.

Implement
mod_inverse(a: int, m: int) → int
Examples
in[3,11]out4
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 25 min
InputExpectedGot
[3,11]4not run yetsample