Code RoomBinary exponentiation modulo
MediumPrep Room Coding #1405

Binary exponentiation modulo

CodingAlgorithms & data structuresMid–Senior~15 min

Implement modular exponentiation: return (a raised to the power b) modulo m, for b up to 10^18, WITHOUT using Python's built-in pow three-argument form. Use binary (square-and-multiply) exponentiation, reducing modulo m at every multiplication to keep numbers small. Return 0 when m == 1.

Implement
mod_pow(a: int, b: int, m: int) → int
Examples
in[2,10,1000]out24
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 15 min
InputExpectedGot
[2,10,1000]24not run yetsample