Code Logo

Harbor Relay Coverage Radius

Published at05 Jan 2026
Easy 4 views
Like30

You have checkpoint positions along a pier and relay positions that can broadcast in both directions. Every relay uses the same radius, and you want the smallest radius that still covers every checkpoint.

A checkpoint is covered if at least one relay is close enough. That means the radius must be at least the distance from that checkpoint to its nearest relay. The final answer is the largest of those nearest distances.

For example, if checkpoints = [1,5,9] and relays = [2,8], the answer is 3. Checkpoint 1 is 1 unit from relay 2, checkpoint 5 is 3 units from the nearest relay, and checkpoint 9 is 1 unit from relay 8. The biggest gap is 3, so a radius of 3 is enough. If relays = [] while checkpoints still exist, the answer is -1 because nothing can cover them.

So the task is to find the nearest relay for each checkpoint, then return the largest distance you had to use.

Example Input & Output

Example 1
Input
checkpoints = [1,5,9], relays = [2,8]
Output
3
Explanation

A radius of 3 covers each checkpoint (1 is within 1 of relay 2, 5 within 3 of 2 or 8, and 9 within 1 of 8).

Example 2
Input
checkpoints = [2], relays = []
Output
-1
Explanation

Without relays, no radius provides coverage.

Example 3
Input
checkpoints = [0,10], relays = [4]
Output
6
Explanation

A radius of 6 reaches both ends from relay position 4.

Algorithm Flow

Recommendation Algorithm Flow for Harbor Relay Coverage Radius
Recommendation Algorithm Flow for Harbor Relay Coverage Radius

Solution Approach

A nice way to solve this is to look at each checkpoint and find its nearest relay. Since the relay positions are sorted, we can use binary search to find where the checkpoint would be inserted.

If that insertion position is idx, then only two relays can possibly be the nearest ones: relays[idx] on the right, and relays[idx - 1] on the left.

const leftDist = idx > 0 ? Math.abs(checkpoint - relays[idx - 1]) : Infinity;
const rightDist = idx < relays.length ? Math.abs(relays[idx] - checkpoint) : Infinity;
const needed = Math.min(leftDist, rightDist);

The value needed is the smallest radius that covers that single checkpoint. Repeat that for every checkpoint, and keep the maximum needed value. That maximum is the minimum radius that covers them all.

If there are checkpoints but no relays, return -1 immediately. Otherwise, this solution runs in O(n log m) time for n checkpoints and m relays.

Best Answers

java
import java.util.*;
class Solution {
    public int calculate_radius(int[] stations, int[] targets) {
        Arrays.sort(stations);
        int maxR = 0;
        for (int t : targets) {
            int idx = Arrays.binarySearch(stations, t);
            if (idx < 0) {
                idx = -(idx + 1);
                int d1 = (idx > 0) ? t - stations[idx-1] : Integer.MAX_VALUE;
                int d2 = (idx < stations.length) ? stations[idx] - t : Integer.MAX_VALUE;
                maxR = Math.max(maxR, Math.min(d1, d2));
            }
        }
        return maxR;
    }
}