Given drone positions on a line and a query position, return the nearest available drone.
You are given the positions of several drones on a number line. Some drones may be unavailable. For a query position, find the available drone whose position is closest to the query.
If two available drones are equally close, choose the one with the smaller position. If no drone is available, return an empty result.
Assume all positions are integers.
Return the position of the nearest available drone, or an empty result / sentinel value if none exists.
Example 1
Input
positions = [10, 3, 7, 20], available = [false, true, true, true], query = 8
Output
7
Explanation
Available drones are at 3, 7, and 20. Distances to 8 are 5, 1, and 12, so 7 is nearest.
Example 2
Input
positions = [5, 9, 2], available = [false, false, false], query = 4
Output
-1
Explanation
No drone is available, so return the sentinel value -1.
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.