Magic Number Transformer
Medium
bfs
shortest-path
graph
number-theory
Problem Description
Transform number A into number B using the minimum number of operations. Available operations: add 1, multiply by 2, subtract 3. Find the minimum steps required to reach B from A.
Input Format
Two integers A and B on separate lines.
Output Format
Minimum number of operations needed to transform A into B, or -1 if impossible.
Constraints
• 1 ≤ A, B ≤ 1000
• Operations: +1, ×2, -3
• Cannot go below 1 during transformation
• If B is unreachable, return -1
• Use BFS for optimal solution
Sample Input/Output
Input:
2 10
Output:
3
Explanation
Transform 2 → 10: Step 1: 2×2=4, Step 2: 4×2=8, Step 3: 8+1=9, Step 4: 9+1=10. Wait, that's 4 steps. Better: 2+1=3, 3×2=6, 6+1=7, 7+1=8, 8+1=9, 9+1=10. Actually optimal: 2×2=4, 4+1=5, 5×2=10. That's 3 steps.
Solution Hints
100
Points
Points
3000ms
Time Limit
Time Limit
256MB
Memory Limit
Memory Limit
Code Editor
Register to Access Code Editor
Create a free account to solve coding problems and track your progress.
Register Now