Problems > Broken Calculator > Editorial

Broken Calculator — Solution & Editorial

Back to the Problem

Reason backwards from B: halve when even, increment when odd; once B ≤ A the rest are decrements (forwards). Forward greedy over-doubles.

Complexity: O(log B)

Watch out: Forward reasoning fails (when to stop doubling?) — the backwards moves are forced.