Ctrl K

Dial Up the Target

Medium
A counter shows the number start, and you want it to show goal. You have a set of step values steps. While the current number x is between 0 and 1000 inclusive, one move picks any step s from steps and replaces x with one of: - x + s; - x - s; - x XOR s (bitwise exclusive OR). Steps can be reused any number of times in any order. A move that takes the number outside 0..1000 is allowed, but the counter then freezes and no further moves are possible. Return the fewest moves that turn start into goal, or -1 if it cannot be done.
Example 1
steps=[3, 6], start=1, goal=10 → 2

1 + 3 = 4, then 4 + 6 = 10. No single move reaches 10.

Example 2
steps=[4], start=0, goal=2 → -1

Every move with step 4 keeps the number a multiple of 4, so 2 is out of reach.

Example 3
steps=[400], start=300, goal=1100 → 2

300 → 700 → 1100. The value 1100 is outside 0..1000, but it is the goal, so that is fine.

Constraints
  • 1 ≤ len(steps) ≤ 1000
  • -10⁹ ≤ steps[i] ≤ 10⁹
  • 0 ≤ start ≤ 1000
  • -10⁹ ≤ goal ≤ 10⁹
  • start ≠ goal
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsDrain the Landlocked Ponds MediumLand Regions on a Map MediumSteps to the Nearest Exit MediumFewest Bills MediumRatio Chain Queries Medium
loading Python…
Input
steps=[3, 6], start=1, goal=10
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 133 tests on the server, hidden ones included.