Problem: Minimum Operations to Convert Number

You are given a 0-indexed integer array `nums` containing distinct numbers, an
integer `start`, and an integer `goal`. There is an integer `x` that is
initially set to `start`, and you want to perform operations on `x` such that
it is converted to `goal`. You can perform the following operation repeatedly
on the number `x`:

If `0 <= x <= 1000`, then for any index `i` in the array (`0 <= i <
nums.length`), you can set `x` to any of the following:

* `x + nums[i]`
* `x - nums[i]`
* `x ^ nums[i]` (bitwise-XOR)

Note that you can use each `nums[i]` any number of times in any order.
Operations that set `x` to be out of the range `0 <= x <= 1000` are valid, but
no more operations can be done afterward.

Return the minimum number of operations needed to convert `x = start` into
`goal`, and `-1` if it is not possible.


Python solution:
import math
import itertools
import bisect
import collections
import string
import heapq
import functools
import sortedcontainers
from typing import List, Dict, Tuple, Iterator

class Solution:
  def minimumOperations(self, nums: List[int], start: int, goal: int) -> int:
    ans = 0
    q = collections.deque([start])
    seen = {start}

    while q:
      ans += 1
      for _ in range(len(q)):
        x = q.popleft()
        for num in nums:
          for res in (x + num, x - num, x ^ num):
            if res == goal:
              return ans
            if res < 0 or res > 1000 or res in seen:
              continue
            seen.add(res)
            q.append(res)

    return -1


Java solution:
class Solution {
  public int minimumOperations(int[] nums, int start, int goal) {
    Queue<Integer> q = new ArrayDeque<>(List.of(start));
    boolean[] seen = new boolean[1001];
    seen[start] = true;

    for (int step = 1; !q.isEmpty(); ++step)
      for (int sz = q.size(); sz > 0; --sz) {
        final int x = q.poll();
        for (final int num : nums) {
          for (final int res : new int[] {x + num, x - num, x ^ num}) {
            if (res == goal)
              return step;
            if (res < 0 || res > 1000 || seen[res])
              continue;
            seen[res] = true;
            q.offer(res);
          }
        }
      }

    return -1;
  }
}


C++ solution:
class Solution {
 public:
  int minimumOperations(vector<int>& nums, int start, int goal) {
    queue<int> q{{start}};
    vector<bool> seen(1001);
    seen[start] = true;

    for (int step = 1; !q.empty(); ++step)
      for (int sz = q.size(); sz > 0; --sz) {
        const int x = q.front();
        q.pop();
        for (const int num : nums) {
          for (const int res : {x + num, x - num, x ^ num}) {
            if (res == goal)
              return step;
            if (res < 0 || res > 1000 || seen[res])
              continue;
            seen[res] = true;
            q.push(res);
          }
        }
      }

    return -1;
  }
};