Code RoomModular inverse
MediumPrep Room Coding #1194

Modular inverse

CodingAlgorithms & data structuresMid–Senior~20 min

Given a prime p and a list of integers, return the modular inverse of each element modulo p, i.e. for each x return the y in [0, p-1] with (x*y) % p == 1. Every input element is guaranteed coprime to p (1 <= x < p). Use Fermat's little theorem. Constraints: p is an odd prime up to 10^9+7, list length up to 1000.

Implement
mod_inverses(p: int, xs: list[int]) → list[int]
Examples
in[7,[1,2,3]]out[1,4,5]
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 20 min
InputExpectedGot
[7,[1,2,3]][1,4,5]not run yetsample