Find the minimum number of jumps needed to reach the end of an array when you can also teleport between prime-valued positions.
You are given an integer array nums of length n. Start at index 0 and want to reach index n - 1 using the fewest moves.
In one move, you may do either of the following:
i to i - 1 or i + 1 if that index exists.i to any other index j such that nums[i] and nums[j] are both prime numbers and the two values satisfy the teleportation rule described by the problem instance.Return the minimum number of moves required to reach the last index.
This is a shortest-path-in-unweighted-state-space problem. The key challenge is to model teleportation efficiently so that repeated prime-to-prime transitions do not become too expensive.
nums: integer array of length n0n - 1Return the minimum number of moves needed to reach index n - 1 from index 0.
1 <= n <= $10^{5}$-1Example 1
Input
nums = [2, 4, 5, 9, 11]
Output
2
Explanation
One optimal route is 0 -> 2 -> 4. The first step uses a teleport-style move between prime-valued positions, and the second step moves forward by adjacency.
Example 2
Input
nums = [1, 6, 8, 10, 12]
Output
4
Explanation
There are no useful prime-teleport options, so you must move by adjacent indices only: 0 -> 1 -> 2 -> 3 -> 4.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.