Skip to content

Practice Minimum Operations to Convert Number

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2059
LeetCode Problem

Minimum Operations to Convert Number

Problem Statement

Start with x = start. While x is between 0 and 1000 inclusive, choose any nums value and replace x by x + value, x - value, or x XOR value. Values may be reused. A result outside the allowed interval is permitted but cannot be changed further. Return the fewest operations reaching goal, or -1 if impossible.

Source: Minimum Operations to Convert Number on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [2,4,12], start = 2, goal = 12
Output
2

# Example 2

Input
nums = [3,5,7], start = 0, goal = -4
Output
2

# Example 3

Input
nums = [2,8,16], start = 0, goal = 1
Output
-1

Constraints

1 <= nums.length <= 1000
-10^9 <= nums[i], goal <= 10^9
0 <= start <= 1000
start != goal
All the integers in nums are distinct.

Related LeetCode Problems

Practice more problems similar to Minimum Operations to Convert Number.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.