All problems
0731HardArrayTwo PointersBinary SearchDynamic ProgrammingGreedyBit ManipulationGraph TheorySorting

Relay Hops Across The Field Network

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3534Path Existence Queries in a Graph II

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A survey team has planted n radio relays, labelled 0 through n - 1. Relay i is tuned to the frequency level[i].

Two relays can pass a message straight to each other exactly when the absolute difference between their frequencies is at most maxGap. That is the only thing that decides it: labels, distance on the ground and the order the relays were planted play no part, and every pair of relays whose frequencies are close enough can pass messages both ways.

Each query queries[t] = [a, b] asks for the smallest number of direct passes a message needs to travel from relay a to relay b. If no chain of passes joins them, the answer for that query is -1. When a and b are the same relay the answer is 0.

Return an array holding the answer to every query, in the order the queries are listed.

Examples

Example 1

Input
n = 3, level = [0, 1, 10], maxGap = 5, queries = [[0, 2], [0, 1], [2, 2]]
Output
[-1, 1, 0]

Relays 0 and 1 are 1 apart, so they pass messages straight to each other. Relay 2 is 9 above relay 1 and 10 above relay 0, both beyond the gap, so nothing joins it to the rest. The last query starts and ends at the same relay.

Example 2

Input
n = 5, level = [0, 2, 4, 100, 102], maxGap = 2, queries = [[0, 2], [0, 4], [4, 3], [2, 0]]
Output
[2, -1, 1, 2]

Relay 0 at 0 and relay 2 at 4 are 4 apart, too far for one pass, but relay 1 at 2 is within 2 of both, so a message can go 0, 1, 2. Relay 4 at 102 and relay 3 at 100 are 2 apart, so they pass directly, and no relay bridges the gap from 4 up to 100.

Example 3

Input
n = 4, level = [7, 7, 7, 7], maxGap = 0, queries = [[0, 3], [1, 1]]
Output
[1, 0]

All four relays share a frequency, so any two of them differ by 0, which a gap of 0 allows. A query between two different relays takes one pass, and one that starts where it ends takes none.

Constraints

  • 1 <= level.length <= 10^5
  • n == level.length
  • 0 <= level[i] <= 10^5
  • 0 <= maxGap <= 10^5
  • 1 <= queries.length <= 10^5
  • queries[i].length == 2
  • 0 <= queries[i][j] <= 10^5 - 1
  • Every label listed in queries is the label of a relay in the network.

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def relay_hop_counts(n: int, level: list[int], maxGap: int, queries: list[list[int]]) -> list[int]:
Java
public int[] relayHopCounts(int n, int[] level, int maxGap, int[][] queries)
September 7
Apply