From A to B
InterviewTime limit1sMemory limit512 MB
Given two integers a and b, find the minimum number of operations (divide an even number by two, or add one) needed to turn a into b.
- Level
Medium5 of 10
- Topics
- Greedy, Bit manipulation, Math, Implementation
- Solved
- No attempts yet
Problem
You are given two integers a and b. You want to turn a into b by performing a sequence of operations. You can only perform the following operations:
- Divide a by two (only when a is even)
- Add one to a
What is the minimum number of operations needed to turn a into b?
Input
The first line contains two space-separated integers a and b. (1 ≤ a, b ≤ 10^9)
Output
Print a single integer: the minimum number of the given operations needed to turn a into b.