Asked • 05/24/19

The shortest path between two integers by adding or subtracting?

You are given two integers a and b. You want to find the shortest sequence of operations necessary to transform a into b, where at each step you are allowed to add or subtract 5, 7, or 12. For example, if you are given a = -5 and b = 19, the shortest path is -5 + 12 + 12 = 19. If you were given 1 and 3, the shortest path would be 1 + 7 - 5 = 2. The only way I can think about solving this is using BFS and maybe some more pruning. Is there a better algorithm I could use instead?

1 Expert Answer


Patrick B. answered • 05/24/19

Math and computer tutor/teacher

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.


Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.