A hardware register must be reconfigured from state a to state b (both given as non-negative integers, compared bit by bit in binary). Toggling a single bit from 0 to 1 draws a calibration pulse costing 1 unit of energy. Toggling a single bit from 1 to 0 requires a discharge cycle costing 2 units of energy. Bits where a and b already agree require no action.
Compute the minimum total energy needed to transform a into b.
Line 1: two space-separated integers a b.
A single integer: the minimum total energy cost.
Example 1
Input
5 3
Expected
3
Explanation
5 is 101 and 3 is 011 in binary. Bit 1 must flip 0→1 (cost 1) and bit 2 must flip 1→0 (cost 2), for a total cost of 3.
Example 2
Input
0 0
Expected
0
Explanation
a and b are already identical, so no flips are needed.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →